Tuesday, June 24, 2008

How to Search a word in word doc

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("D:\pavan\Word.doc") 'replace the file with your MSDoc
searchString = "pavan" '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


Regards,

PavanKumar Nandagiri...............

Google Search

Google