O'Reilly logo

Writing Word Macros, Second Edition by Steven Roman PhD

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The HeaderFooter Object

A HeaderFooter object represents either a header or a footer. A HeadersFooters collection contains HeaderFooter objects of one type (header or footer) or the other, but not both.

A HeadersFooters collection, whether it be a collection of headers or footers, is indexed by one of the constants in the following enum:

	Enum WdHeaderFooterIndex
	   wdHeaderFooterPrimary = 1
	   wdHeaderFooterFirstPage = 2
	   wdHeaderFooterEvenPages = 3
	End Enum

As you can see, there are three types of headers and three types of footers. (I discuss the meaning of a primary header or footer a bit later in this chapter.)

Thus, to obtain the HeaderFooter object that represents the primary header in the first section of the active document, write:

	ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary)

Similarly, the primary footer in this section is:

	ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary)

As an example, the following code changes the text in both the primary header and the primary footer:

	With ActiveDocument.Sections(1)
	 .Headers(wdHeaderFooterPrimary).Range.Text = "Header text"
	 .Footers(wdHeaderFooterPrimary).Range.Text = _
	      "Footer text"
	End With

Note that if the current selection is within a header or footer, the HeaderFooter property of the Selection object will return that HeaderFooter object.

Different Odd and Even Headers or Footers

The PageSetup object has two properties that relate to headers and footers: DifferentFirstPageHeaderFooter and OddAndEvenPagesHeaderFooter.

You can ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required