MS Word, Use Visual Basic to Highlight words?

ecuguru
ecuguru used Ask the Experts™
on
I've got a VB array of words that are important to me.
I'd like to figure out some sort of a visual markup that lets the user identify those words easily.  I was thinking that highlight would be a good use.  

1) Given a list of words, is there a way to highlight them all in a given word document?

2) When the user goes to close the document, it will probably ask them if they want to save the changes they made to the document even if they didn't make any, because of the highlighting.  I don't really want this to happen, so does anyone also know the line of code for clearing all highlighting?  Or even better, clearing the highlighting the other macro made?  Maybe making an undo is a better course..
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Retired
Top Expert 2012
Commented:
Here are a couple of word macros that show how to do the two tasks
Option Explicit
Sub HighlightWords()
    Dim strWords() As String
    Dim i As Integer
    Dim wrdDoc As Word.Document
    Dim wrdApp As Word.Application
    strWords = Split("brown,jumps,dog", ",")
    Set wrdApp = Application
    'Set wrdDoc = wrdApp.Documents.Open("C:\MyFolder\MyFile.doc")
    Set wrdDoc = ActiveDocument
    For i = 0 To UBound(strWords)
        With wrdDoc.Range.Find
            .Text = strWords(i)
            .Replacement.Highlight = True
            .Execute Replace:=wdReplaceAll
        End With
    Next i
End Sub
 
Sub RemoveHighlighting()
    Dim wrdDoc As Word.Document
    Dim wrdApp As Word.Application
    
    Set wrdApp = Application
    'Set wrdDoc = wrdApp.Documents.Open("C:\MyFolder\MyFile.doc")
    Set wrdDoc = ActiveDocument
    With wrdDoc.Range.Find
        .Highlight = True
        .Replacement.Highlight = False
        .Execute Replace:=wdReplaceAll
    End With
End Sub

Open in new window

Joanne M. OrzechManager, Document Services Center
Top Expert 2004

Commented:
ecuguru

Did GrahamSkan's macro resolve your question?  If so, please close out and assign points.  Thank you.

Joanne Orzech
EE Zone Advisor

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial