February 1st, 2006

How Can I Boldface a Specific Word Throughout a Microsoft Word Document?

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I boldface a specific word throughout a Microsoft Word document?

— SB

SpacerHey, Scripting Guy! AnswerScript Center

Hey, SB. You’ll probably find this hard to believe, but when you work with scripts and scripting all day you tend to get a little jaded. You say you have a script that automatically backs up and clears all the event logs on all your computers? How nice. You have a script that monitors folders and lets you know when new files are added? Been there, done that. You have a script that can bring the dead back to life? Hey, who doesn’t?

For some reason, though, SB, your question piqued our interest. Maybe it’s because we’re always looking for a script that uses Microsoft Word; scripting with Word is actually kind of fun. Or maybe it’s because this request offered a bit of a challenge: although we were pretty sure that we could write a script that would boldface specific words in a document we’d never actually tried it. Or maybe it’s because all the dead people we brought back to life have been pretty insistent that we go through their documents and boldface specific words. You know, things like the phrase I am too alive.

Oh, well. Regardless of what motivated us, here’s what we came up with:

Const wdReplaceAll  = 2

Set objWord = CreateObject(“Word.Application”) objWord.Visible = True

Set objDoc = objWord.Documents.Open(“C:\Scripts\Test.doc”) Set objSelection = objWord.Selection

objSelection.Find.Text = “Fabrikam” objSelection.Find.Forward = TRUE objSelection.Find.MatchWholeWord = TRUE

objSelection.Find.Replacement.Font.Bold = True

objSelection.Find.Execute ,,,,,,,,,,wdReplaceAll

As you can see, not only was this a fun script to write but – as an added bonus – it was an easy script to write, too. After defining a constant named wdReplaceAll (we’ll talk about that in a moment), we create an instance of the Word.Application object and then set the Visible property to True; this gives us an instance of Microsoft Word that we can see on screen. We use the Open method to open the document C:\Scripts\Test.doc, then create an instance of the Word Selection object (which, by default, positions the cursor at the beginning of the document).

Now we’re ready to roll. What we want to do is boldface any instances of the word Fabrikam; hence we set the value of the Find object’s Text property to “Fabrikam” (in other words, that’s what we’re searching for). Next we set the Forward property to True; this ensures that our search will start at the beginning of the document and end at, well, the end of the document. Finally, we set the MatchWholeWord property to True; we do this just in case there happens to be some crazy construction like FabrikamCorporation in the document. If MatchWholeWord was set to False, then any instance of the string Fabrikam would be boldfaced. That would give us something similar to this: FabrikamCorporation. Which we probably don’t want.

Note. Yes, we are kind of zipping through the explanation here, aren’t we? But that’s OK: after all, we have an Office Space article that discusses the process of finding and replacing text in more detail.

All that gives us a mechanism for finding all instances of the word Fabrikam. But once we find them how do we boldface each of those instances? Here’s how:

objSelection.Find.Replacement.Font.Bold = True

Yes, it’s that easy. Here we use the Replacement object (a child object of the Find object) and specify that the Font.Bold property should be True. That’s all we have to do. If we wanted to un-boldface each instance of Fabrikam we would set the Font.Bold property to False. Needless to say, we could specify new replacement text, a new replacement font size, pretty much anything we want.

See why we like scripting with Microsoft Word so much?

After configuring the replacement object we then call the Execute method to start the find-and-replace operation:

objSelection.Find.Execute ,,,,,,,,,,wdReplaceAll

And, no, we didn’t fall asleep with our finger on the comma key. Well, not this time anyway. As it turns out, there are tons of optional parameters available for the Execute method. For this script we didn’t care about any of those. However, because parameters have to be specified in order, we couldn’t just do something like this:

objSelection.Find.Execute wdReplaceAll

That would make the constant ReplaceAll (which tells Word we want to replace all instances of our search text) the first parameter passed to the Execute method, which would cause the script to fail. Hence all the “empty” parameters represented by the commas. And don’t worry: if that doesn’t make any sense to you, take a look at the documentation for the Execute method and you’ll see a list of all the parameters we skipped.

OK. That was fun, wasn’t it? In fact, we’d love to stay and chat about it some more, but Abe Lincoln and Genghis Khan are both out of iced tea. You know, writing a script to bring the dead back to life really wasn’t all it’s cracked up to be ….

Author

0 comments

Discussion are closed.