How do I setup macros to find and highlight words?

B

Brendiux Rea

I am creating a macro in word 2002 to find certain words in all document and
highlight them. Word was able to do it, however, when I try to record the
macro, it does not work.
 
G

Greg Maxey

Something like this should get you started:

Sub myHighlighter()
Dim myWord As String
Dim rngStory As Word.Range

myWord = InputBox("Type word you want to find and highlight:")

For Each rngStory In ActiveDocument.StoryRanges
FindAndHighlight rngStory, myWord
Set rngStory = rngStory.NextStoryRange
Next
End Sub
Sub FindAndHighlight(ByVal rngStory As Word.Range, myWord As String)

Do Until (rngStory Is Nothing)
With rngStory.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = myWord
While .Execute
rngStory.HighlightColorIndex = wdBrightGreen
Wend
End With
Set rngStory = rngStory.NextStoryRange
Loop
End Sub

I have an addin that you can use it you like:

http://gregmaxey.mvps.org/Find_it_tool_bar.htm
 
K

Klaus Linke

Brendiux Rea said:
I am creating a macro in word 2002 to find certain words in all
document and highlight them. Word was able to do it, however,
when I try to record the macro, it does not work.


Hi Brendiux,

And if "highlight" means "select", you're pretty much out of luck.

The new item (Select all instances in:...) isn't exposed in the dialog's
arguments, and there's next to no VBA support for multiple selections (...
none at all for Find).

The only option would be to use SendKeys to remote control opening the
dialog and choosing the right settings.
That might work if you only use the macro yourself, but is very fragile.

:-( Klaus
 
B

Brendiux Rea

Thank you Greg,
I am trying to create a macro that identifies about 50 words (I will setup
them) and highlight them. I am using the following path:

Edit/find what word --> write the word that I am looking for
check mark "Highlight all items found in"
After word selects the word, I highlight them with yellow color
 

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