How do I find a word everytime that it appears in my Word documen.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I would like to be able to locate a specific word every time it appears in a
document
 
If you just want to find then Edit>Find and type in the word.

The following two macros might be useful. The first finds and highlights
the specified word and count occurences. The second clears the
hightlighting.

Sub CountOccurrences()
Dim fWord
Dim iCount As Integer
Dim Reset As VbMsgBoxResult

fWord = InputBox$("Type in the word or phrase that you want to find and
count.")
iCount = 0

Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:=fWord, Wrap:=wdFindStop, _
MatchWholeWord:=True, Forward:=True) = True
Selection.Range.HighlightColorIndex = wdYellow
Selection.Collapse wdCollapseEnd
iCount = iCount + 1
Loop
End With
If iCount > 1 Then
MsgBox Chr$(34) & fWord & Chr$(34) & " was found " & _
iCount & " times."
ElseIf iCount = 1 Then
MsgBox Chr$(34) & fWord & Chr$(34) & " was found " & _
iCount & " time."
Else
MsgBox Chr$(34) & fWord & Chr$(34) & " was not found."
End If
ActiveDocument.Variables("fWord").Value = fWord


End Sub
Sub ClearHL()
Dim fWord As String
Selection.HomeKey wdStory
Selection.Find.ClearFormatting

fWord = ActiveDocument.Variables("fWord").Value

With Selection.Find
Do While .Execute(FindText:=fWord, Wrap:=wdFindStop, _
MatchWholeWord:=True, Forward:=True) = True
Selection.Range.HighlightColorIndex = wdNoHighlight
Selection.Collapse wdCollapseEnd
Loop
End With
End Sub
 
Ctrl+F to open the Find dialog. Check the box for "Highlight all items found
in" and select the appropriate range.

--
Suzanne S. Barnhill
Microsoft MVP (Word)
Words into Type
Fairhope, Alabama USA

Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 

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

Back
Top