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.

Adding Page Numbers to a Microsoft Word Document

The truth is, there’s no reason to use Microsoft Word as a way to create fancy-looking reports unless you’re going to tap the capabilities of Word and, well, create fancy-looking reports. That doesn’t necessarily mean you have to fill your reports with WordArt and clip art and the most garish font colors ever devised; it does mean, however, that you should add those nice – and useful – little touches that aren’t available with plain-text files. Things like page numbers.

But, you protest, isn’t it hard to add page numbers to Word? Actually, it’s not hard at all. For example, here’s a script that adds page numbers to the footer of a document, using the default values. If you’d prefer to add page numbers to the header of a document, then find this line of code:

Set objPageNumbers = objSection.Footers(1).PageNumbers

And change it to this:

Set objPageNumbers = objSection.Headers(1).PageNumbers

Here’s what the script looks like:

On Error Resume Next

Const wdPageBreak = 7

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

Set objSection = objDoc.Sections(1)
Set objSelection = objWord.Selection

Set objPageNumbers = objSection.Footers(1).PageNumbers
objPageNumbers.Add(1)

objSelection.TypeText "This is page 1."
objSelection.InsertBreak(wdPageBreak)
objSelection.TypeText "This is page 2."
objSelection.InsertBreak(wdPageBreak)
objSelection.TypeText "This is page 3."

The script begins by defining a constant named wdPageBreak; we’ll use this to insert a couple of page breaks in the document. Before you ask, this isn’t part of the process for adding page numbers; instead we just wanted our sample script to create a multi-page document, the better for you to see what your page numbers will look like.

Next we create an instance of the Word.Application object, set the Visible property to True (so we can see the finished document), and then use the Add method to create a new document. Now we’re ready to add some page numbers.

To do that we first create an object reference named objSection that represents section 1 in our document. (This particular example has only one section.) We also create an object reference (objSelection) to the Selection object; we’ll use that later on to add some text to our document. Those two operations are carried out in these two lines of code:

Set objSection = objDoc.Sections(1)
Set objSelection = objWord.Selection

All we need now are two lines of code that add the page numbers. In line 1, we create an instance of the PageNumbers object, specifying that we want to add the page numbers to the first footer in the document:

Set objPageNumbers = objSection.Footers(1).PageNumbers

And then in line 2 we call the Add method to add the page numbers to the document:

objPageNumbers.Add(1)

That’s it; the rest of the script just adds some text and a few page breaks to the document, the better to give us a multi-page document to look at. The end result is a document with page numbers that look like this:

Microsoft Word

Of course, we can get much fancier with our page numbers. For example, here’s a table that shows you a few additional formatting options:

Property

Description

NumberStyle

Indicates the type of number we want to use. The constant wdPageNumberStyleUppercaseRoman (value 1) gives us uppercase Roman numerals: I, II, III, IV, etc.

ShowFirstPageNumber

Determines whether a page number appears on the first page of the document. When set to False, no page number appears on page 1; when set to True, a page number does appear on page 1.

RestartNumberingAtSection

Causes page numbers to restart at each new section in the document. This value must be set to True when specifying a different StartingNumber.

StartingNumber

Specifies the page number to be assigned the first page in the document (or section). In the following script, StartingNumber is set to 10, meaning the first page will be numbered page 10 (or, in uppercase Roman numerals, page X).

Here’s a revised script that includes all the options shown in the preceding table:

On Error Resume Next

Const wdPageBreak = 7
Const wdPageNumberStyleUppercaseRoman = 1

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

Set objSection = objDoc.Sections(1)
Set objSelection = objWord.Selection

Set objPageNumbers = objSection.Footers(1).PageNumbers
objPageNumbers.NumberStyle = wdPageNumberStyleUppercaseRoman
objPageNumbers.ShowFirstPageNumber = FALSE
objPageNumbers.RestartNumberingAtSection = TRUE
objPageNumbers.StartingNumber = 10
objPageNumbers.Add(1)

objSelection.TypeText "This is page 1."
objSelection.InsertBreak(wdPageBreak)
objSelection.TypeText "This is page 2."
objSelection.InsertBreak(wdPageBreak)
objSelection.TypeText "This is page 3."

As you can see, we create the PageNumbers object and then configure the property values for that object (NumberStyle, ShowFirstPageNumber, etc.). After the property values have been configured, we call the Add method to add the custom-formatted page numbers to the document.

Speaking of which, here’s what our fancy page numbers look like:

Microsoft Word