How Can I Change the Default Highlight Color for a Microsoft Word Document?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I change the default highlight color for a Microsoft Word document?

— TW

SpacerHey, Scripting Guy! AnswerScript Center

Hey, TW. You know, any time the Scripting Guy who writes this column wants to terrify the Scripting Son, he tells the blood-curdling saga of the first TV the Scripting Dad remembers: a TV that showed everything in black-and-white only! Granted, that was a long time ago, and the Scripting Dad has only vague memories of that TV; not too long after he was born his Scripting Parents bought a color TV. But it was real nonetheless, and just the thought of having to watch everything in black-and-white is enough to give the Scripting Son the heebie-jeebies.

And no, the Scripting Dad doesn’t mention the fact that there were no remote controls in those days, either. It’s one thing to give the Scripting Son a little scare every now and then; it’s quite another thing to terrify and traumatize the poor kid.

Note. We also should point out that the fact that the Scripting Dad has only vague memories of this TV doesn’t mean that all of this happened hundreds of years ago. After all, the Scripting Dad has only vague memories of what he had for breakfast this morning, despite the fact that – Mondays through Fridays – he pretty much has the same thing for breakfast every morning.

The important thing here – beyond the fact that kids these days have no idea how good they really have it – is the fact that color is important, regardless of whether we’re talking about TVs or Microsoft Word documents. So can you change the default highlight color for a Word document? Well, as long as you start that document with a script you can:

Const wdBrightGreen = 4
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add()
Set objOptions = objWord.Options
objOptions.DefaultHighlightColorIndex = wdBrightGreen
objWord.Visible = True

As you can see, we start out by defining a constant named wdBrightGreen and assigning it the value 4. Needless to say, we do that because we want to set the default highlight color to bright green. (Hey, when you start out life watching TV in black-and-white any color looks good, even bright green.) What if you consider bright green to be a bit garish? No problem; you can use any of the constants and values shown in the following table:

Constant

Value

wdAuto

0

wdBlack

1

wdBlue

2

wdBrightGreen

4

wdByAuthor

-1

wdDarkBlue

9

wdDarkRed

13

wdDarkYellow

14

wdGray25

16

wdGray50

15

wdGreen

11

wdNoHighlight

0

wdPink

5

wdRed

6

wdTeal

10

wdTurquoise

3

wdViolet

12

wdWhite

8

wdYellow

7

After defining the constant we use these two lines of code to create an instance of the Word.Application object and open a new, blank document:

Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add()

As soon as we have a document we’re ready to change the default highlight color. To do that we need to do two things: 1) create an instance of Word’s Options object, and 2) assign the constant wdBrightGreen to the Option object’s DefaultHighlightColorIndex property. That’s what we do here:

Set objOptions = objWord.Options
objOptions.DefaultHighlightColorIndex = wdBrightGreen

That’s all we have to do. If we make our instance of Word visible (which we do, in the last line of the script) and if you look on the toolbar you’ll see that the default highlight color is now bright green. How ‘bout that?

What the preceding script does is set the default highlight color; if you now manually highlight text in a Word document and then click the highlight button that text will be highlighted in bright green. However, if you want to highlight text programmatically you don’t need to set the default highlight color. Instead, you can use a script similar to this:

Const wdBrightGreen = 4
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add()
objWord.Visible = True
Set objSelection = objWord.Selection
objSelection.TypeText "Here is text that is not highlighted."
objSelection.TypeParagraph()
objSelection.Range.HighlightColorIndex = wdBrightGreen
objSelection.TypeText "And here's text highlighted in green"
objSelection.TypeParagraph()

Let’s see if we can quickly run through this script and figure out what it does. We start out – again – by defining a constant named wdBrightGreen and setting the value of that constant to 4. We then create an instance of Word, add a new document, and then make that instance of Word visible onscreen. That’s all old hat by now. (Isn’t it?)

Our next step is to create an instance of the Word Selection object, then use the TypeText and TypeParagraphs() methods to type a paragraph (a paragraph of non-highlighted text) into our document. That’s what we do here:

Set objSelection = objWord.Selection
objSelection.TypeText "Here is text that is not highlighted."
objSelection.TypeParagraph()

What we want to do now is type in some text that is highlighted. To accomplish that feat all we need to do is use the Selection object’s Range object and set the value of the HighlightColorIndex property to wdBrightGreen:

objSelection.Range.HighlightColorIndex = wdBrightGreen

Until we tell the Range object something different, all the text we type from this point on will be highlighted in bright green. In fact, when our script finishes running our Word document will look like this:

Microsoft Word

Cool, huh? To turn highlighting off just set the HighlightColorIndex to 0:

objSelection.Range.HighlightColorIndex = 0

Hope that helps, TW. As for the Scripting Son, well, we’re going to go easy on him. We thought about telling him about bottles of Pepsi that couldn’t be opened by hand but required a bottle opener instead. Or maybe the fact that, way back when, you could only watch cartoons on Saturday mornings; there was no such thing as 24-hour cartoon channels. But that would be just plain mean, wouldn’t it?

Besides, he probably wouldn’t believe us anyway.

0 comments

Discussion is closed.

Feedback usabilla icon