highlighting text in a word document

W

wolfgangea

I have a list of different words in an excel speadsheet that I want to search
in a word document and if it finds that word to highlight it in red
throughout the whole document. If possible I would load these words into an
array and go through a loop to find the occurences. I do not have to use
excel, the list can be imported into word.
 
C

Cooz

Hi wolfgangea,

Make a copy of the xls file, move the list of words to the first column on
the first worksheet and save this copy as 'Text (Tab delimited) (*.txt)'.

In Word, record any macro, say record clicking the B-button on the Standard
toolbar, and be sure to choose the document under 'Store macro in'.
Choose Tools | Macro > Macro's..., select your macro and choose Edit. The
VBA-editor opens.
Replace the entire macro by the following:

Sub HighlightWords()
Dim strWord As String

Selection.HomeKey Unit:=wdStory

Open "G:\temp\ListOfWords.txt" For Input As #1
While Not EOF(1)
Line Input #1, strWord

With Selection.Find
.ClearFormatting
.Text = strWord
.Forward = True
.Wrap = wdFindContinue
.MatchWholeWord = True
.Execute
End With

While Selection.Find.Found
Selection.Range.HighlightColorIndex = wdRed
Selection.Find.Execute
Wend

Selection.HomeKey Unit:=wdStory
Wend
Close #1

End Sub

Replace "G:\temp\ListOfWords.txt" by the .txt file you just made.
Click the Save button in the VBA editor to save the document. Close the VBA
editor. In Word, choose Tools | Macro > Macros... Select HighlightWords and
click Run.

Good luck,
Cooz
 

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