VBA Word Highlight Text

I wish to highlight every occurence of several words in a document.

Something like .......

strFind = "cat, sat, mat"                     ' This list may vary and is populated by a text box

With ActiveDocument.Range.Find
    .ClearFormatting
    For xxx = 0 To UBound(Split(strFind, ","))
        .Text = Split(strFind, ",")(xxx)
        .Highlight = True

    Next
End With

Then every occurrence of cat, sat and mat will be highlighted

Can you help with the required code?

Answer
Answer

It works here also and if you change the line

.Text = Split(StrFnd, ",")(i)

to

.Text = Trim(Split(StrFnd, ",")(i))

it won't matter if you include the space

What highlight colour is set? add the line

Options.DefaultHighlightColorIndex = wdYellow

before

With ActiveDocument.Range.Find

Graham Mayor (Microsoft Word MVP 2002-2019)
For more Word tips and downloads visit my web site
https://www.gmayor.com/Word_pages.htm

1 person found this reply helpful

·

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated November 25, 2024 Views 6,530 Applies to: