Micro Focus QTP (UFT) Forums
Replace a string in a word document with another string - Printable Version

+- Micro Focus QTP (UFT) Forums (https://www.learnqtp.com/forums)
+-- Forum: Micro Focus UFT (earlier known as QTP) (https://www.learnqtp.com/forums/Forum-Micro-Focus-UFT-earlier-known-as-QTP)
+--- Forum: UFT / QTP Beginners (https://www.learnqtp.com/forums/Forum-UFT-QTP-Beginners)
+--- Thread: Replace a string in a word document with another string (/Thread-Replace-a-string-in-a-word-document-with-another-string)



Replace a string in a word document with another string - rekha.naik - 09-19-2010

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


RE: Replace a string in a word document with another string - venkatbatchu - 09-19-2010

Hi Rekha,

Please use Replace Function...


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


Please let me know for further clarrification...



Regards,
Venkat.Batchu


RE: Replace a string in a word document with another string - rekha.naik - 09-19-2010

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)
***********************************************************************************


RE: Replace a string in a word document with another string - Saket - 09-22-2010

use word object model instead using Filesystemobject for handling a docx file.


RE: Replace a string in a word document with another string - rekha.naik - 09-30-2010

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
*********************************************************************


RE: Replace a string in a word document with another string - cdesserich - 10-01-2010

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



RE: Replace a string in a word document with another string - rekha.naik - 10-05-2010

Thank you. This worked Smile


RE: Replace a string in a word document with another string - manjunathb - 11-14-2013

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>;

Thanks and Regards,
Manju


RE: Replace a string in a word document with another string - pranikgarg - 11-14-2013

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.