May 4, 2012

Preparing MS Word text with a specific highlight color

If the Catholic Church decides its needs an official backup for Jerome as the patron saint of translators (and in these times of tribulation, one cannot have enough divine help I suppose), Dave Turner of ASAP Traduction gets my vote. His CodeZapper macros for Microsoft Word have saved us so many thousands of hours of grief dealing with rogue tags in RTF and MS Word files, which screw up TM and termbase matches and make work very difficult, and other more recent contributions also offer useful support. He is the first one in my mind when I see a problem and think "There ought to be a macro for that!"

Dave's latest contribution was part of an old discussion about preparing texts for translation in which the text to translate was marked by a highlight color. As I remember the original discussion, there were several highlight colors, and only one was to be chosen for work. Usually, if text of a certain color is to be hidden or shown in preparing a file for translation with a CAT tool which filters out hidden text, I use the search and replace function in Microsoft Word. That does not work for selecting a highlight of a specific color. You need to use a macro for that, and I no longer have the VBA skills to handle that myself. I can adapt a working macro, but no way I can manage a good one from scratch unless I spend a few days or more re-learning the skills of a decade ago.

So I was very happy when I saw his answer to the problem in the memoQ yahoogroups forum, which I have reproduced here with just a  minor change to reflect the usual highlighting I encounter:

Sub HideExceptYellow()
' Translation assistance macro
' by Dave Turner

Dim rDcm As Range
ActiveDocument.Range.Font.Hidden = True
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = ""
.Highlight = True
.Forward = False
While .Execute
If rDcm.HighlightColorIndex = wdYellow Then
rDcm.HighlightColorIndex = wdNoHighlight
rDcm.Font.Hidden = False
rDcm.Collapse Direction:=wdCollapseStart
rDcm.Start = ActiveDocument.Range.Start
End If
End With
Set rDcm = ActiveDocument.Range
Options.DefaultHighlightColorIndex = wdYellow
With rDcm.Find
.Text = ""
.Font.Hidden = False
.Forward = False
.Replacement.Highlight = True
.Execute Replace:=wdReplaceAll
End With
End Sub
In MS Word 2003 and earlier versions, the macro can be created under Tools > Macros > View Macros. Name the macro, then click the Create button to paste in the code. The Run button will execute an existing macro if it is selected.

In MS Word 2007/2010 the same functionality is accessed on the View ribbon with the Macros icon or Alt+F8.

Here's a short video showing the procedure to copy the code into the Normal global template in Microsoft Word, where it is available to all open documents in Word:

To adapt this for another highlight color, just rename the macro and change the designation of the color (wdYellow). The macro can be adapted to deal with combinations of highlight colors as well, and similar methods can be used to deal with text colors, though these can be handled by the search and replace dialog.


  1. This doesn't work in Word 2010

  2. Really? That's news to me. Word 2010 is all I've got these days. But then I suppose nuclear power is safe too....

  3. Very impressed with this macro, thanks.
    I wanted to hide only the yellow text, which I managed to do, but then the recursion (or whatever) seems to struggle and fall over on my really big documents. thanks though ...


Notice to spammers: your locations are being traced and fed to the recreational target list for my new line of chemical weapon drones :-)