Using VBA Code to Export Highlighted Text of a Particular Color
top of page

Using VBA Code to Export Highlighted Text of a Particular Color


You can make use of a macro to select all of the text highlighted in a specific color in a Word document. The macro itself turns text highlighted in a selected color to a different font color - in this example red. This will help you export out (or copy just the text highlighted in one color) because you can search for text with in a particular font color, but not text highlighted in a particular color.

Press ALT + F11 to enter Visual Basic and enter the code below (posted to Word Banter by Dawn Crosler) in a new module

On the line reading:

If Selection.Range.HighlightColorIndex = wdYellow Then

. . . you can designate the color highlighting you want to process. See a list of different highlighting colors in Crosler's post.

Once the color of the text has been changed you can run a search in Word for just text with the font color red. Use the option for Find In . . . Main Document.

All of the search results will be selected. You can then copy them out for analysis.

Sub ChangeHighlight()

'Purpose: Changes highlight colors in a doc...need to change

'WD color index

'Changes the font color based on selected highlight color

'***********************

'go to top of doc

Selection.HomeKey Unit:=wdStory

'clear previous find

Selection.Find.ClearFormatting

'set search for highlight on

Selection.Find.Highlight = True

'run search

Selection.Find.Execute

'while highlights are still found

While Selection.Find.Found = True

'if highlight is "wdBrightGreen" (color to find) then

'change the wdBrightGreen in the line below to match the Color

'of highlight you desire. Use the wdColorIndexConstant List

'as inspiration

If Selection.Range.HighlightColorIndex = wdYellow Then

'change Selected Text Font to White

'Use the wdColorIndexConstant List to change to the

'appropriate font color

Selection.Range.Font.Color = wdColorRed

End If

'continue same search

Selection.Find.ClearFormatting

Selection.Find.Highlight = True

Selection.Find.Execute

'end loop when no more found

Wend

'move back to top of doc when done

Selection.HomeKey Unit:=wdStory

End Sub


bottom of page