Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / March 2006

Tip: Looking for answers? Try searching our database.

highlighting text in a word document

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
wolfgangea - 08 Mar 2006 21:41 GMT
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.
Cooz - 09 Mar 2006 13:51 GMT
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
--
PS: If this is a satisfying answer to your question and you're logged in via
the Microsoft site, please click Yes to "Did this post answer the question?".
Thanks.

> 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.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.