Sunteți pe pagina 1din 6

Automating MS Word « Quick test professional Page 1 of 6

Quick test professional


QTP Tips QTP codes QTP Faqs and more
 Home
 Mohan Kumar Kakarla

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

http://quicktestprofessional.wordpress.com/category/automating-ms-word/ 5/4/2011
Automating MS Word « Quick test professional Page 2 of 6

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

February 14, 2008 Posted by quicktestprofessional | Automating MS Word | Automating word,


Automation Testing, Microsoft Word Automation, QTP, VBS, VBScripting | 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 quicktestprofessional | Automating MS Word | 1 Comment

How to search for a specific string in a Microsoft


Word document

http://quicktestprofessional.wordpress.com/category/automating-ms-word/ 5/4/2011
Automating MS Word « Quick test professional Page 3 of 6

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

http://quicktestprofessional.wordpress.com/category/automating-ms-word/ 5/4/2011
Automating MS Word « Quick test professional Page 4 of 6

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 quicktestprofessional | Automating MS Word | 9 Comments

Mohan Kumar Kakarla

http://quicktestprofessional.wordpress.com/category/automating-ms-word/ 5/4/2011
Automating MS Word « Quick test professional Page 5 of 6

 Archives
 March 2011
 November 2010
 November 2008
 October 2008
 September 2008
 June 2008
 March 2008
 February 2008

 Search for: Search

 Blog Stats
 617,432 hits

 Archives
 March 2011 (1)
 November 2010 (1)
 November 2008 (4)
 October 2008 (1)
 September 2008 (2)
 June 2008 (1)
 March 2008 (4)
 February 2008 (44)
 Categories
 Automated Test Script Creation Process
 Automating MS Word
 Automating QC
 Automation Object Model For QTP
 Backward compatability in QTP
 Browser Scripts
 COM and QTP
 compare 2 text files
 Descriptive programming
 Descriptive Programming and Child Objetcs

http://quicktestprofessional.wordpress.com/category/automating-ms-word/ 5/4/2011
Automating MS Word « Quick test professional Page 6 of 6

 Dictonary Object
 DotNetFactory
 Excel Automation
 Extra Topics
 Files and Folders
 General Standards to be followed in Test scripts
 New Features In QTP9.5
 QC-OTA
 QTP Additional Faqs
 QTP Faqs
 QTP Naming Convention
 QTP Tips and Faqs
 Running stored procedures from QTP
 Send keyboard keys
 Uncategorized
 Uninstall a software using QTP
 Uninstall a software using vbscript
 Update data in a database
 VBScript and IE Automation
 VBScript Faqs and Useful resources
 What’s New in Quick Test Pro 9.0
 Whats New in QTP 9.5
 XML Scripting
 RSS
Entries RSS
Comments RSS

Site info
Quick test professional
Theme: Andreas04 by Andreas Viklund. Blog at WordPress.com.

http://quicktestprofessional.wordpress.com/category/automating-ms-word/ 5/4/2011

S-ar putea să vă placă și