Micro Focus QTP (UFT) Forums

Full Version: Replace a string in a word document with another string
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
Hi Rekha,

Please use Replace Function...


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


Please let me know for further clarrification...



Regards,
Venkat.Batchu
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)
***********************************************************************************
use word object model instead using Filesystemobject for handling a docx file.
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
*********************************************************************
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
Thank you. This worked Smile
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
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.