How Can I Add Centered Page Numbers to the Footer of a Word Document?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! I need to put the page number (centered) in the footer of a Word document. Do you have any suggestions on how I can do that?

— AW

SpacerHey, Scripting Guy! AnswerScript Center

Hey, AW. To tell you the truth, the Scripting Guy who writes this column no longer makes suggestions. That’s because of an incident that occurred a few years ago, when someone he works with sent out a blanket email about their latest project, adding, “Please let me know if you have any suggestions on how we can make this even better.” Being a bit naïve in the ways of the world, our hero actually had a couple suggestions, so he helpfully forwarded those along.

You got it: big mistake. After being chewed out by the person who sent the email (“A lot of people put a lot of time and effort into this project and all you can do is find things that are wrong with it!”) and that person’s manager and his manager (“You should know better than to give her any suggestions”), this particular Scripting Guy vowed never to make another suggestion as long as he lived, a vow he has stuck to for nearly three years now.

Also a vow which, interestingly enough, hasn’t seemed to upset anyone else in the least. No one seems to miss his help at all.

So, no, AW, we don’t have any suggestions for you; we don’t do suggestions. Instead, the best we can do is offer you a script that puts the page number (centered) in the footer of a Word document:

Const wdAlignPageNumberCenter = 1

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

objDoc.Sections(1).Footers(1).PageNumbers.Add(wdAlignPageNumberCenter)

Yes, it is a simple little script, isn’t it? We start out by defining a constant named wdAlignPageNumberCenter and setting the value to 1; this tells the script that we want the page number centered within the footer. What if we change our mind and decide that we don’t want the page number centered? That’s OK; we can use any of the following constants and their values:

Constant

Value

wdAlignPageNumberCenter

1

wdAlignPageNumberInside

3

wdAlignPageNumberLeft

0

wdAlignPageNumberOutside

4

wdAlignPageNumberRight

2

After defining our constant we use these three lines of code, which create a visible instance of the Word.Application object and then give us a new, blank document to work with:

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

Once we’ve done that we can add the page numbers with a single line of code:

objDoc.Sections(1).Footers(1).PageNumbers.Add(wdAlignPageNumberCenter)

What we’re doing here is creating a PageNumbers object tied to the first footer in the first, and only, section of our document; that’s what objDoc.Sections(1).Footers(1) is all about. At the same time, we’re calling the Add method to add page numbers to the footer, passing along the constant wdAlignPageNumberCenter to indicate that we want the page numbers centered. And that, as they say, is that.

Of course, there are plenty of other options we can apply to page numbers, far more than we can cover in this one column. (For more information, check out the Microsoft Word VBA Language Reference on MSDN.) Just to whet your appetite, however, we’ll give you one example. The following script defines a constant named wdPageNumberStyleLowercaseRoman and then assigns that constant to the NumberStyle property:

Const wdAlignPageNumberCenter = 1
Const wdPageNumberStyleLowercaseRoman = 2

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

objDoc.Sections(1).Footers(1).PageNumbers.Add(wdAlignPageNumberCenter) objDoc.Sections(1).Footers(1).PageNumbers.NumberStyle = wdPageNumberStyleLowercaseRoman

What this will do is – sorry, what was that? Wow; you’re right: that displays page numbers as lowercase Roman numerals. Very perceptive of you.

Hey, wait a second: you guys aren’t cheating and reading ahead are you? That’s what we thought.

Here, by the way, are the other allowed page number styles. Bear in mind that, depending on your language settings, some of these might not make much sense (or make much difference in your document):

Constant

Value

wdPageNumberStyleArabic

0

wdPageNumberStyleArabicFullWidth

14

wdPageNumberStyleArabicLetter1

46

wdPageNumberStyleArabicLetter2

48

wdPageNumberStyleHanjaRead

41

wdPageNumberStyleHanjaReadDigit

42

wdPageNumberStyleHebrewLetter1

45

wdPageNumberStyleHebrewLetter2

47

wdPageNumberStyleHindiArabic

51

wdPageNumberStyleHindiCardinalText

52

wdPageNumberStyleHindiLetter1

49

wdPageNumberStyleHindiLetter2

50

wdPageNumberStyleKanji

10

wdPageNumberStyleKanjiDigit

11

wdPageNumberStyleKanjiTraditional

16

wdPageNumberStyleLowercaseLetter

4

wdPageNumberStyleLowercaseRoman

2

wdPageNumberStyleNumberInCircle

18

wdPageNumberStyleNumberInDash

57

wdPageNumberStyleSimpChinNum1

37

wdPageNumberStyleSimpChinNum2

38

wdPageNumberStyleThaiArabic

54

wdPageNumberStyleThaiCardinalText

55

wdPageNumberStyleThaiLetter

53

wdPageNumberStyleTradChinNum1

33

wdPageNumberStyleTradChinNum2

34

wdPageNumberStyleUppercaseLetter

3

wdPageNumberStyleUppercaseRoman

1

wdPageNumberStyleVietCardinalText

56

And, yes, now that you mention it sometimes it is hard to go through the day without making any suggestions. After all, this Scripting Guy has been making suggestions for 40 years, and never once has anyone taken him up on one of those suggestions. That’s like Cal Ripken’s record for consecutives games played in Major League baseball: it’s inevitable, but you still hate to see a streak like that come to an end. (Although, like boxer Rocky Marciano, we suppose there’s also merit in retiring with a perfect record.)

0 comments

Discussion is closed.

Feedback usabilla icon