Share via


Office Space: Tips and Tricks for Scripting Microsoft Office Applications

Office Space

Welcome to Office Space, the new column that offers tips and tricks for scripting Microsoft® Office applications. We’ll post new tips every Tuesday and Thursday; to see an archive of previous tips, visit the Office Space Archive. And if you have particular questions about Microsoft Office scripting, feel free to send them to scripter@microsoft.com (in English, if possible). We can’t promise to answer all the questions we receive, but we’ll do our best.

Appending Text to a Microsoft Word Document

A little while ago we published an Office Space column that showed people how to append data to a Microsoft Excel spreadsheet. The response to that particular column was overwhelming:

“Hey, how do I append text to a Microsoft Word document?”

“That’s nice, but I need to know how to add data to the end of a Word document.”

“Is there any way to do something similar with a Word document?”

“Dear friend. You do not know me, but I am the widow of the former Defense Minister of Freedonia. Before he died my husband deposited $50 million in a secret bank account. He also asked me to find out if there is any way to append text to a Microsoft Word document.”

OK, you don’t have to beat us over the head with a stick (although many people would like to do that). Let’s say we have a simple little Word document, one that looks like this:

Microsoft Word

We need a script that can open this document and append new data to the end. Does such a script exist? Well, it does now:

Const wdStory = 6
Const wdMove = 0

Set objWord = CreateObject("Word.Application")
objWord.Visible = True

Set objDoc = objWord.Documents.Open("c:\scripts\testdoc.doc")

Set objSelection = objWord.Selection
objSelection.EndKey wdStory, wdMove

objSelection.TypeParagraph()
objSelection.TypeParagraph()
objSelection.TypeText "This text was appended to an existing Word document."

A lot easier than you thought it would be, isn’t it? The script starts out by defining two constants: wdStory and wdMove. wdStory tells Word where we want to move the selection point (not surprisingly, the value 6 means “move the selection point to the end of the story,” which is Word-talk for “the end of the document”). wdMove tells the script to actually move the selection; in other words, we want the little flashing cursor to jump from its default location at the start of the document to the very end of the document. Alternatively, we could have defined a constant named wdExtend and set the value to 1. In that case we would have extended the selection from the beginning of the document to the end; in other words, we would have selected the entire document.

We had a feeling someone would ask that. Yes, we can move (or extend) the selection to other spots in the document; we aren’t limited just to jumping to the end of the document. Here are a few of the places we can go:

Constant

Value

wdSentence

3

wdParagraph

4

wdLine

5

wdStory

6

For more information, see the Microsoft Word VBA Language Reference on MSDN.

But back to business. After defining our two constants we create an instance of the Word.Application object and then set Visible property to True (just so we can see everything on screen). We use the Open method to open the file C:\Scripts\Testdoc.doc, and then create an instance of the Selection object. By default, this places the selection point at the very beginning of the document.

But we want the selection point moved to the end of the document, don’t we? (After all, any text we add is going to be added wherever the selection point happens to be.) To do that we use the EndKey method, which, as the name implies, emulates the use of the END key on the keyboard. Here’s the line of code that moves the selection point to the end of the document:

objSelection.EndKey wdStory, wdMove

As you can see, we pass EndKey two parameters: wdStory (which tells the script to move the selection point to the end of the document) and wdMove, which clarifies that we want to move the selection point and not extend the selection. After calling the EndKey method our little flashing cursor will now be at the end of the document rather than at the beginning of the document.

Note. What if wanted to emulate the HOME key instead? You guessed it: we’d use the HomeKey method.

The rest of the code is there just to demonstrate that we actually did move to the end of the document. We use the TypeParagraph() method to add a couple of carriage return-linefeeds to the document (to separate our new text from our old text) and then use this line of code to actually append text to the file:

objSelection.TypeText "This text was appended to an existing Word document."

And – wonder of wonders – our Word document will now look like this, with the new text tacked on to the end of the old text:

Microsoft Word

There you have it: appending text to a Microsoft Word document. Now, at long last, we can all just sit back and relax.

What do you mean, now how do you append a new slide to a PowerPoint presentation…?