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?

1 Like

Played around with this idea, and the challenge is actually inserting the styled text. I have tried inserting rich text with TextExpander, but I cannot get it to revert back to the destination font after the snippet has been inserted.

In attached, what would be the most efficient way of inserting the red font colour text?

I wonder whether what @Nige_S has in mind is not so much the insertion of styled text as the application of a user-defined style.

Once you have flagged one or more phrases with the name of a user defined character style, you can change the appearance of every style-flagged phrase at once, by editing the definition of the named style.

One way of creating a user-defined character style name in MS Word:

Format > Style... > List: > User-defined styles > New ... > Style type: > Character

and in the form you can choose a Name: like "Bird", and a character colour like Red.

Thanks I'm not at my desk at the moment to test that. But first thoughts are how do I flag which words require the user defined style? My documents have hundreds of instances of bird names and I need to avoid manually flagging each instance.

If the creation of new paragraphs is continuing, then that probably divides into two issues:

  1. tagging all bird names in the existing text with the user-defined Bird style
  2. entering new bird names

For the tagging of bird names occurring in the existing text, based on an external list of such names, I'll aim to send you something to test over the weekend.


In the meanwhile, note that MS Word Edit > Find > Advanced Find and Replace ... can be used to apply a named style to every occurrence of a particular phrase.


For tagging bird names in new paragraphs, as you type, there are various options, including, perhaps, a bit of Keyboard Maestro automation, but one approach is to:

  • Keep the MS Word styles pane visible
  • click on the name of the Bird style to apply that style to selected text

I have four ideas. We translators have to deal with a lot of Ms Word documents where we have to do a lot of substitutions. Here are four approaches:

  1. Rename the docx files to docx.zip and extract the relevant XML files. Open them in BBEdit and do global replacements in all open files, adding tags of your choice (e.g. ‹example›) to the bird names. You may want to do this from a list of names. Once finished, put the XML files back. Rename from .docx.zip to docx.
  2. Import the .docx files into a translation editor (CAT tool) and do the search and replace here. Some CAT tools allow you to use search and replace lists. (e.g. CafeTran Espresso.
  3. Use the paid Transtools to do the multiple replacements directly in Ms Word. Windows only!
  4. Use the free Plustools to perform the multiple replacements directly in Ms Word. Windows only!

Plustools seems to be offline at the moment. I mean this version:

After tagging the bird names. You have to reopen all Ms Word docx and apply character styles to all text between "'‹" and "›'". This can possibly be done via TransTools, but it should not take too much time to do it manually.

Is there a way to get Keyboard Maestro to insert a word into a Microsoft Word document that is styled with "Bird" style, therefore avoiding having to click the Styles pane each time I type a bird name? This would really slow down typing speed.

The first thing to decide might be how you want to choose or identify which bird name you want to insert or get into the clipboard.

Or to put it another, how would you like to indicate that you are:

  1. Starting the typing / pasting of a bird name
  2. Ending the typing / pasting of a bird name, and continuing in default character style.

Technically, it might be possible to set up 5000 style-applying autocorrect entries in MS Word, but if memory or hardware are at all constrained, that might create more friction than it removed. MS Office memory management is not very deft or athletic.

Could indicate I am inserting a bird name using a typed string trigger, as in attached.

Edit the Style, give it a shortcut key. You may want to do the same for the "Normal" style so you can easily turn the style off again.

1 Like

Thanks I'll try adding that as a keystroke action to typed string trigger I posted above. Then another keystroke action to revert back to "Normal". Think that could work well.

I have assigned ⌃⇧B to turn on "bird name" style and macro in screenshot succeeds in doing that. But the format will not revert back to normal after the bird name has been inserted, even though I have assigned ⌃⇧N to "Normal". The text colour stays red. How can I get the font to revert back to normal after bird name has been inserted?

Apologies -- it's been a long time since I played with Word Styles...

Character styles ("BirdName") override paragraph styles ("Normal"). So you'll need to set yourself a "Body" (or similarly-named) Character style, based on the "Normal" style but without the formatting tweaks that "BirdName" has.

You probably already have a style called "Body Text" that will do the job, but it might not show in the Styles pane unless you show "All styles".

Give "plain" style the keyboard shortcut and you'll easily be able to switch between the two.

Perhaps triggering on bird names would be complicated by the need for thousands of such triggers ?

The more feasible possibility suggested by @Nige_S is, I think, that you make use of MS Word's ability to assign a keystroke to a user-defined style name.

Screenshot 2025-01-17 at 12.39.19 pm

If you:

  • assign one keystroke to the default Bird style, and
  • another keystroke to the default (Normal) style

then you can invoke the bird style as you start to type a bird name, and close it off when you finish.

Not keen on pressing the keyboard shortcuts as would increase key presses a lot. To type bird names, I already use TextExpanders like "xop". So using "xop" as a Keyboard Maestro string trigger would mean no extra key presses.

But even though I have following Nige_S's recommendation and made a "Body" style, I still cannot get the above macro to revert back to black font. The font stays red.

Check your style carefully, particularly for anything it might be inheriting. You may have to explicitly set the font colour -- styles are easiest to manage when you use them from the start and use them in preference to ad hoc formatting.

But you'll find a way to get it working, just poke about. With a "BirdName" style:

...and a "Body" style:

...this macro:

image

...produces this in Word:

image

1 Like