VB code for searching a particular word in a word document.

C

c.piyush

The number of words in the word document can be many.
I have written the code for opening a document and then finding a
particular word but i don't know how to navigate the whole word
document for that particular word and highlight it.

This is what i've written till now:

Private Sub Command1_Click()
Dim appWord As Word.Application
Dim wrdDoc As Word.Document
Dim strFileName As String
Dim strFind As String
strFind = "Piyush"
strFileName = "C:\Test1.doc"
Set appWord = New Word.Application
Set wrdDoc = appWord.Documents.Open(strFileName)
appWord.Selection.Find.Text = strFind
If appWord.Selection.Find.Execute = True Then
MsgBox ("Text found.")
Else
MsgBox ("The text could not be located.")
End If
Set wrdDoc = Nothing
Set appWord = Nothing
End Sub

Kindly look into this and help complete me code.
Thanks.
 
T

TC

I see lots of hits for the following google groups search:

"selection.find.text" loop

Have you loOKed at any of those?

HTH,
TC
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top