QTP:- Working with Word Documents examples
Working with
Word Docs
a) Create a word document and enter some data
& save
Dim objWD
Set objWD =
CreateObject("Word.Application")
objWD.Documents.Add
objWD.Selection.TypeText "This is some text." & Chr(13)
& "This is some more text"
objWD.ActiveDocument.SaveAs
"e:\kalyaneddy.doc"
objWD.Quit
b) How to search for a particular string in MS
word document?
For this
particular script I have a word document saved in C drive with some paragraphs
of text and I am searching for word 'qtp' in that word file. The search is case
insensitive. I ran it using MS Word 2003.
Dim word_app
Dim word_doc
Dim
str_to_search
'Create the
Word Object
Set word_app
= CreateObject("Word.Application")
Set word_doc
= word_app.Documents.Open("C:\wrd.doc")
str_to_search
= "QTP"
For p = 1 To
word_doc.Paragraphs.Count
'A Range
object represents a contiguous area in a document, defined by a starting
character position and an ending character position. The contiguous area can be
as small as the insertion point or as large as the entire document. It can also
be, but does not have to be, the area represented by the current selection.
'When you
use the Range method to specify a specific area of a document, you use the
method's Start argument to specify the character position where the range
should begin and you use the End argument to specify where the range should
end.
'Using the
Start and End properties with a Range object, you can create a new Range object
that refers to a group of document elements.
startext_range =
word_doc.Paragraphs(p).Range.Start
endRange = word_doc.Paragraphs(p).Range.End
Set text_range = word_doc.Range(startext_range,
endRange)
text_range.Find.Text = str_to_search
Next
'close the document
word_doc.Close
'close the Word application
word_app.Quit
Set word_doc = Nothing
Set word_app = Nothing
c) How to
Find & Replace a string in MS word document?
Const
wdFindContinue = 1
Const
wdReplaceAll = 2
Set ob_word
= CreateObject("Word.Application")
Set ob_doc =
ob_word.Documents.Open("C:\wrd.doc")
Set
ob_selection = ob_word.Selection
ob_selection.Find.Text
= "QTP"
'This is the
direction we want to search. Because we created the Selection object right at
the beginning of the document we want to go forward (that is, towards the end
of the document). Hence we set the value of the Forward property to True.
ob_selection.Find.Forward
= True
ob_selection.Find.MatchWholeWord
= True
'wdFindContinue:
The find operation continues when the beginning or end of the search range is
reached.
ob_selection.Find.Wrap
= wdFindContinue
ob_selection.Find.Format
= False
ob_selection.Find.Replacement.Text
= "Mercury"
'wdReplaceAll
tells the script to automatically replace all instances of the word
ob_selection.Find.Execute
,,,,,,,,,,wdReplaceAll
ob_doc.Close
ob_word.Quit
Set ob_doc =
Nothing
Set ob_doc =
Nothing
d) Script
will display all the doc files in all the drives in the system
Dim mw
Set
mw=CreateObject("Word.Application")
Set
fs=createobject("Scripting.FileSystemObject")
Set
d=fs.Drives
mw.FileSearch.FileName="*.doc"
For each dr
in d
msgbox dr
mw.FileSearch.LookIn=dr
mw.FileSearch.SearchSubFolders=True
mw.FileSearch.Execute
For each i
in mw.FileSearch.FoundFiles
print i
Set
f=fs.GetFile(i)
print
f.Name&" "&f.Size&" "&f.DateCreated
print
"-------------------------------------------------------------------"
Next
Next
mw.Quit
e) Create
word, Create table and write all the services names
Set mw =
CreateObject("Word.Application")
mw.Visible =
True
Set dc =
mw.Documents.Add()
Set objRange
= dc.Range()
dc.Tables.Add
objRange,1,3
Set objTable
= dc.Tables(1)
x=1
strComputer
= "."
Set
wms=GetObject("winmgmts:\\" & strComputer &
"\root\cimv2")
Set colItems
= wms.ExecQuery("Select * from Win32_Service")
For Each s
in colItems
If x > 1
Then
objTable.Rows.Add()
End If
objTable.Cell(x,
1).Range.Font.Bold = True
objTable.Cell(x,
1).Range.Text = s.Name
objTable.Cell(x,
2).Range.text = s.DisplayName
objTable.Cell(x,
3).Range.text = s.State
x = x + 1
Next