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
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
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.
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.
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
-
Archives
- July 2011 (1)
- March 2011 (1)
- November 2010 (1)
- November 2008 (4)
- October 2008 (2)
- 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
- 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