Quick test professional

QTP Tips QTP codes QTP Faqs and more

How to search for a specific string in a Microsoft Word document

How to search for a specific string

in a Microsoft Word document  

Use the Find Microsoft Word Object Model object

You can refer to the Microsoft Word 2000 Visual Basic Reference for a complete listing of Word methods and properties that can be used within a QuickTest Professional (QTP) script. You can use these Word object methods within a QTP script to create documents, edit documents, spell check, etc.For a complete listing of Word object’s methods and properties, refer to MSDN Library – Microsoft Word Object Model.

Note:
Microsoft Word’s object methods are not part of QuickTest Professional, therefore, they are not guaranteed to work and are not supported by Mercury Customer Support. Any changes that Microsoft may make to these methods are not the responsibility of Mercury.
These examples are not part of QuickTest Professional. They are not guaranteed to work and are not supported by Mercury Customer Support. You are responsible for any and all modifications that may be required.

The following example uses Word object methods to open a Microsoft Word Document and to use the Find object (the Find and Replace functionality) to search for the word “apple.”

Example:
Dim wrdApp
Dim wrdDoc
Dim tString, tRange
Dim p, startRange, endRange
Dim searchString

‘Create the Word Object
Set wrdApp = CreateObject(“Word.Application”)
Set wrdDoc = wrdApp.Documents.Open(“C:\Temp\SampleWord.doc”) ‘replace the file with your MSDoc
searchString = “apple” ‘replace this with the text you’re searching for

With wrdDoc
For p = 1 To .Paragraphs.Count
startRange = .Paragraphs(p).Range.Start
endRange = .Paragraphs(p).Range.End
Set tRange = .Range(startRange, endRange)
‘ tString = tRange.Text
tRange.Find.Text = searchString
tRange.Find.Execute

If tRange.Find.Found Then
msgbox “Yes! ” & searchString & ” is present”
End If

Next
.Close ‘ close the document
End With
wrdApp.Quit ‘ close the Word application
Set wrdDoc = Nothing
Set wrdApp = Nothing

The following example uses Word object methods to open a Microsoft Word Document and retrieve paragraphs from it. Then the InStr VBScript method is used to check for the word “apple.”

Example:
Dim wrdApp
Dim wrdDoc
Dim tString, tRange
Dim p, startRange, endRange
Dim searchString

Create the Word Object
Set wrdApp = CreateObject(“Word.Application”)
Set wrdDoc = wrdApp.Documents.Open(“C:\Temp\Text.doc”) ‘replace the file with your MSDoc
searchString = “apple” ‘replace this with the text you’re searching for

With wrdDoc
For p = 1 To .Paragraphs.Count
startRange = .Paragraphs(p).Range.Start
endRange = .Paragraphs(p).Range.End
Set tRange = .Range(startRange, endRange)
tString = tRange.Text
tString = Left(tString, Len(tString) – 1) ‘exclude the paragraph-mark
If InStr(1, tString, searchString) > 0 Then ‘ check if the text has the content you want
‘ some other processing here
msgbox “Yes! ” & searchString & ” is present”
End If
Next
.Close ‘ close the document
End With
wrdApp.Quit ‘ close the Word application
Set wrdDoc = Nothing

 

  Source: Mercury Forum’s KB articles

Advertisements

February 14, 2008 - Posted by | Automating MS Word

13 Comments »

  1. hi,
    first i am saying very thaks to u. You r given very use ful information for us.

    Comment by manohar | April 4, 2008 | Reply

  2. Hi,

    The information regarding MS Word is very useful to me,
    but in QTP 9.5 the statement (objSelection.EndKey END_OF_STORY) is giving an error Bad Parameter. plz give solution to this……………

    Thanks in advance

    Comment by vidyasagar | May 6, 2008 | Reply

  3. vidyasagar ,
    Sorry for that. It was my mistake only. I forgot to declare the value to the constant END_OF_STORY

    Add the following stmt at the top
    Const END_OF_STORY = 6

    Comment by quicktestprofessional | May 7, 2008 | Reply

  4. Hi,

    I need to talk to you. I have a project and I need help in doing that. We can discuss more on that. You can reach me @ 818-917-2128. Or send me your contact information I will call you.

    Regards,
    Naveen

    Comment by Naveen | May 29, 2008 | Reply

  5. Hi,

    Iam krishna need help form you as below
    Step1 get the strings from the webUI to word document.
    step2 compare the strings with the word document(which i have make is as expected strings) with the strings which i got from the webUI(make it as actual) and the difference in the strings should be dislayed in the different excel

    Thanks in advance for u help

    Regards,
    Krishna kumar

    Comment by krishna kumar | June 4, 2008 | Reply

  6. Longobardi,

    How u r trying to convert.By recording it inQTP and open word document and saveAs to .txt.

    if you are follows the abouve , it will create jusnk data along with actual data.
    so better use Automation Object model for MSWord and open the document , read it line by line , same time create Automation Object model for text file(Scripting.FileSystemObject) and write the data line by line got it from word. is that sounds good?

    Comment by quicktestprofessional | July 3, 2008 | Reply

  7. Thanks for giving such a usefull information.

    Comment by Bharat | December 5, 2008 | Reply

  8. Hi can u pls help me on how to read from word and copy the data into a variable

    Comment by revathi | May 26, 2009 | Reply

  9. Hi,
    I see you do have lot of posting related to MS word automation. These are really helpful.

    I was trying to create a new word doc but before doing that I was trying to check if specific named word doc exists or not but no luck. Can you please tell me how can I check the existence of specific word file.

    If you want you can email me the solution directly. Thank you in adcvance for your help.

    Comment by Mohammed Razzaque | February 20, 2010 | Reply

  10. You have used const Endofstory=6,
    Can you explain me wat it ll does…?

    Comment by venkat | August 13, 2010 | Reply

  11. that was good script , if there is a paragraph in a web page then how to find a string in that
    can u please…
    uday.

    Comment by uday | June 12, 2012 | Reply

  12. The body also gets some important antioxidants from these powerful fruits.
    ‘ Natural cleanser; blood, colon; strengthens gall bladder and liver.
    Store any extra servings in fridge for later in the day.

    Comment by smoothie recipes with 7up | January 5, 2014 | Reply

  13. Hi,

    Can you please let know the script how to an object file(i.e., Excel file,PDF File as Icon) in word file using qtp script

    Comment by Vamsi | March 10, 2014 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: