Quick test professional

QTP Tips QTP codes QTP Faqs and more

How to create tables in Microsoft word

How to create tables in Microsoft word 
Set objWord = CreateObject("Word.Application")
const END_OF_STORY=6
objWord.Visible = True
 
Set objDoc = objWord.Documents.Add()
Set objSelection = objWord.Selection
 
objSelection.TypeText "Table 1"
objSelection.TypeParagraph()
 
Set objRange = objSelection.Range
objDoc.Tables.Add objRange, 1, 2
Set objTable = objDoc.Tables(1)
 
objTable.Cell(1, 1).Range.Text = "This is cell 1."
objTable.Cell(1, 2).Range.Text = "This is cell 2."
objSelection.EndKey END_OF_STORY
objSelection.TypeParagraph()
 
objSelection.TypeText "Table 2"
objSelection.TypeParagraph()
 
Set objRange = objSelection.Range
objDoc.Tables.Add objRange, 1, 2
Set objTable = objDoc.Tables(2)
 
objTable.Cell(1, 1).Range.Text = "This is cell 1."
objTable.Cell(1, 2).Range.Text = "This is cell 2."
 
objSelection.EndKey END_OF_STORY
objSelection.TypeParagraph()
Author: Mohan Kakarla
Advertisements

February 14, 2008 Posted by | Automating MS Word | , , , , , | 4 Comments

Creating a Microsoft Word document

Creating a Microsoft Word document  

Example:
Dim objWD

‘ Create the Word Object
Set objWD = CreateObject(“Word.Application”)

‘ Create a new document
objWD.Documents.Add

‘ Add text to the document
objWD.Selection.TypeText “This is some text.” & Chr(13) & “This is some more text”

‘ Save the document
objWD.ActiveDocument.SaveAs “c:\temp\mydoc.doc”

‘ Quit Word
objWD.Quit

‘ Release the Word

February 14, 2008 Posted by | Automating MS Word | 1 Comment

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

February 14, 2008 Posted by | Automating MS Word | 13 Comments