Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Replace a string in a word document with another string
#1
Solved: 10 Years, 8 Months ago
Hi,

I am a beginner to QTP. I'm trying to write a script for replacing the word "QTP" with "Mercury" in a word document in location "D:\QTP Practice" in my local machine.

The word document "QTP.doc" has the following data in it.

*******************************************************
For testing QuickTest pro for searching a QTP word in the word document QTP .Searching for a word QTP from QTP, QTP .
Test again for the word QTP .
*******************************************************

I tried various methods of searching for the word and replacing it. Looks like I'm missing out on some important point.

Kindly help me with the script.

Thanks
Reply
#2
Solved: 10 Years, 8 Months ago
Hi Rekha,

Please use Replace Function...


Replace("Expression","Find","Replacement")


Please let me know for further clarrification...



Regards,
Venkat.Batchu
Reply
#3
Solved: 10 Years, 8 Months ago
Hi,

Thanks for your response. Below is what I have tried doing.But it is not working. I'm very new to QTP. If you can help me correct it, it would be good.

The below code is not working. It is not replacing the word "QTP" with "Mercury". Opening the file for Writing (Set first_file = FS.GetFile(Doc1Path).OpenAsTextstream(ForWriting,0)) is clearing the worddoc. So I'm stuck.

***********************************************************************************
Code:
Dim Doc1Path
const ForReading = 1

Doc1Path = "D:\QTP Practice\QTP.docx"

Public Function ReplaceWord(Doc1Path)
  Set FS = CreateObject("Scripting.FileSystemObject")
  Set first_file = FS.GetFile(Doc1Path).OpenAsTextstream(ForReading,0)

  Do while first_file.AtEndOfStream =False
       str1=first_file.Read(1000)
       Replace str1,"QTP","Mercury"

Loop

  first_file.Close()
End Function

ReplaceWord(Doc1Path)
***********************************************************************************
Reply
#4
Solved: 10 Years, 8 Months ago
use word object model instead using Filesystemobject for handling a docx file.

Reply
#5
Solved: 10 Years, 8 Months ago
I tried below pasted code and it did not replace the string.

*********************************************************************
Code:
strsearch = "QTP"

Set obj_word = CreateObject("Word.Application")
Set obj_doc = obj_word.documents.Open("D:\QTP Practice\QTP.docx")

For p=1 to obj_doc.Paragraphs.count
    startrange = obj_doc.Paragraphs(p).Range.Start
    endrange=obj_doc.Paragraphs(p).Range.End
    Set  text_range = obj_doc.Range(startrange,endrange)

     text_String = text_range.Text
     strlength = Len(text_String) - 1
     text_String = Left(text_String, strlength )
    

      If InStr(1, text_String, strsearch) > 0 Then
          pos =  InStr(1, text_String, strsearch)
          msgbox pos

         Replace text_String,strsearch,"Mercury"
        End If

Next

obj_doc.close
obj_word.Quit
Set obj_doc = Nothing
Set obj_word = Nothing
*********************************************************************
Reply
#6
Solved: 10 Years, 8 Months ago
This should work better, and you weren't calling save:

Quote:Set obj_word = CreateObject("Word.Application")
Set obj_doc = obj_word.documents.Open("D:\QTP Practice\QTP.docx")
Set obj_words = obj_doc.Words

For i = 1 To obj_words.Count
obj_words.Item(i).Text = Replace(obj_words.Item(i).Text, "QTP", "Mercury")
Next

obj_doc.Save
obj_doc.Close
obj_word.Quit
Set obj_words = Nothing
Set obj_doc = Nothing
Set obj_word = Nothing
Reply
#7
Solved: 10 Years, 8 Months ago
Thank you. This worked Smile
Reply
#8
Solved: 10 Years, 8 Months ago
Hi experts,

I want to replace <system name>from an URL, how can i do this.

http://<system name>:8080/<suffix>

Here i need to replace <system name> with some IP address <10.127.0.0>

my new link should look like
http://10.127.0.0:8080/<suffix&gt;

Thanks and Regards,
Manju
Reply
#9
Solved: 10 Years, 8 Months ago
Hi Manjunathb,

See there are lot of processes to solve your query. I am giving you simplest method to do this:

Dim URL : URL = "http://<system name>:8080/<suffix>

Dim RplString : RplString = "10.127.0.0"

Msgbox Replace(URL, "<system name>", RplString)

Check this.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Split function in string nidhishnair 13 63,195 07-07-2020, 03:47 PM
Last Post: helmzshelmz
  String Array Declaration Bhuvana 0 888 01-07-2020, 12:59 PM
Last Post: Bhuvana
  How to remove the spaces from a string Anand 7 28,427 08-12-2019, 04:47 PM
Last Post: sreekanthP
  String Manipulation engr.awaiszafar@gmail.com 0 1,367 10-11-2018, 11:44 AM
Last Post: engr.awaiszafar@gmail.com
  Split a string with multiple delimiters in VBScript nandha 2 7,882 02-10-2018, 06:44 PM
Last Post: nandha

Forum Jump:


Users browsing this thread: 1 Guest(s)