Highlight words in a Microsoft Word document

I have a long (120k words) Microsoft Word document that has bird names scattered throughout. I need to highlight (in green) all bird names in the document. To do this I use a VBA macro that reads bird names (5,000 different names) from a different document and finds the words in the active document. However, it is very slow – it takes 2hrs for the macro to complete.

Wondered if there might be a faster way to highlight bird names in my Microsoft Word by using Keyboard Maestro?

If the VBA approach which you are using from within MS Word is turning out to be slow, then using the same approach from KM will not speed things up (Keyboard Maestro would have to work across the osascript interface, from outside MS Word, which would add some overhead.

You are probably better off either:

  1. Getting some advice from a VBA forum on what other approaches you could take in VBA, or
  2. Working directly with the MS Word XML (or possibly, if the document does not involve much special formatting, with a plain text version of the data)

(FWIW If I had the word file and bird list in front of me I might get the two files into the most lightweight format that works (perhaps using Pandoc) and write a script in something like Python to bring the bird list into memory as a hashed dictionary, before mapping a conditional emphasis markup over the list of words in the document)

1 Like

If this is a one-off, does that matter?

If this is something you need to change often (green today, nothing tomorrow, red the day after) consider making that first search one that applies a character Style, eg "BirdName". Once you've "tagged" all the names with that style you just need to edit the style definition for a global format change.

Note that you can't apply an actual "Text Highlight" using a Style. What you do is apply shading in the "Borders and Shading" format options. That is in many ways better because you can also change the text colour so it pops.

1 Like

There is something inherently quadratic at work here - document words * bird names.

My guess is that that may be amplifying any inefficiency in the way that the full set of bird names is repeatedly considered, for each additional word of the text.

Just testing each word for membership of a pre-cooked Python or JS Set object might be worth experimenting with - though multi-word bird names might call for a full text scan for every name :frowning:

This is a great idea. Simply make a 'birdname' style, ensure all bird names are written using this style, then simply search for that style when editing. Why didn't I think of that?