August 8th, 2006

How Can I Replace Text in a Microsoft Word Document?

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I search a Microsoft Word document for all occurrences of the string <computername> and replace those occurrences with the actual name of a computer (e.g., atl-ws-01)?

— WC

SpacerHey, Scripting Guy! AnswerScript Center

Hey, WC. And by the way, thanks for your question. Microsoft employees will be getting their yearly performance reviews back sometime in the next few weeks; in a completely unrelated matter, the Scripting Guy who writes this column needs a way to open a Word document and replaces all instances of the word underachieved with the word overachieved. Your question gives him a chance to practice those search-and-replace skills.

So how do you search a Word document for all occurrences of the string <computername> and replace it with the actual name of a computer? Here’s how:

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 = “<computername>” objSelection.Find.Forward = TRUE objSelection.Find.MatchWholeWord = TRUE

objSelection.Find.Replacement.Text = “atl-ws-01”

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

As you can see, it’s not a too terribly complicated script (with the possible exception of that weird-looking final line of code). We start out by defining a constant named wdReplaceAll and setting the value to 2; we’ll use this constant to ensure that we replace each and every instance of <computername> found in the document. Following that we create an instance of the Word.Application object and then – just so we can watch the excitement unfold onscreen – we set the Visible property to True. Finally, we open the document C:\Scripts\Test.doc and use this line of code to create an instance of the Selection object (which has the effect of positioning our cursor at the very start of the document):

Set objSelection = objWord.Selection

Got all that? Good. Because now it’s time to roll up our sleeves and get serious.

Note to managers engaged in yearly performance reviews. That, of course, is just a figure of speech. The Scripting Guy who writes this column is always serious, always has his sleeves rolled up, and is always working.

Pretty much.

As it turns out, the Selection object has a child object named Find; this child object is used to find text in a document. (Unlike the Scripting Guy’s own child object, whose purpose in life – other than spending dad’s money – remains unknown.)

Before we can start the search, however, we need to configure a few properties of the Find object:

objSelection.Find.Text = “<computername>”
objSelection.Find.Forward = TRUE
objSelection.Find.MatchWholeWord = TRUE

As you can probably guess, the Text property represents the text we want to search for. The Forward property specifies the direction of the search; we want to go forward, which simply means that we want to start searching from our current location (the beginning of the document) and continue until we reach the end of the document. MatchWholeWord means that we want to match only entire words. In this case that’s probably not all that important. However, suppose we were searching for the word cat. In that case, if MatchWholeWord was False we’d not only match cat, but also catalog, category, catacomb, vacation, and any other word containing the string cat. Like we said, in this case it probably doesn’t matter: we don’t know of very many words that have <computername> embedded in them. But just to be on the safe side ….

As it turns out, the Find object also has a child object: the Replacement object. For our purposes, the Replacement object has only one function: we use it to specify the replacement text. Because we want to replace all instances of <computername> with atl-ws-01 we set the value of the Replacement object’s Text property to, well, atl-ws-01:

objSelection.Find.Replacement.Text = “atl-ws-01”

After all that buildup it takes just one line of code to carry out the search and replace:

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

We won’t explain all those commas following the Execute method; if you’d like to know more about those take a look at this Office Space column. Suffice to say that the Execute method takes a number of parameters, each of which must be passed in order. The commas simply represent parameters that we don’t need in this script; however, we must pass blank parameters to ensure that wdReplaceAll appears in the proper spot. And that’s all we have to do.

Or at least all you have to do. We still have to modify the script so it replaces underachieved with overachieved. But you don’t need to worry about that.

Note. The Scripting Editor has suggested we tell people that the Scripting Guy who writes this column has no intention of cheating on his performance review. But that’s easy for her to say: she’s a highly-qualified and highly-competent employee. Unless he cheats, the Scripting Guy who writes this column could only get a passing score in one way: by working hard and doing a good job. What do you suppose the odds are of that happening?

Author

0 comments

Discussion are closed.