Class HeaderFooter (Word VBA)

The class HeaderFooter represents a single header or footer. The HeaderFooter object is a member of the HeadersFooters collection. The HeadersFooters collection includes all headers and footers in the specified document section. To use a HeaderFooter class variable it first needs to be instantiated, for example


Dim hfr as HeaderFooter
Set hfr = Selection.HeaderFooter

For Each

Here is an example of processing the HeaderFooter items in a collection.


Dim hfrFooter As HeaderFooter
For Each hfrFooter In Footers
	
Next hfrFooter

Exists

True if the specified HeaderFooter object exists.

The primary header and footer exist in all new documents by default. Use this method to determine whether a first-page or odd-page header or footer exists. You can also use the DifferentFirstPageHeaderFooter or OddAndEvenPagesHeaderFooter property to return or set the number of headers and footers in the specified document or section.


Dim secTemp As Section 
 
Set secTemp = ActiveDocument.Sections(1) 
If secTemp.Headers(wdHeaderFooterFirstPage).Exists = True Then 
 secTemp.Headers(wdHeaderFooterFirstPage).Range.Text = _ 
 "First Page" 
End If

Index

Returns a WdHeaderFooterIndex that represents the specified header or footer in a document or section. Possible return values are wdHeaderFooterEvenPages - Returns all headers or footers on even-numbered pages, wdHeaderFooterFirstPage - Returns the first header or footer in a document or section, wdHeaderFooterPrimary - Returns the header or footer on all pages other than the first page of a document or section.


Sub ChangeFirstPageFooter() 
 Dim hdrFirstPage As HeaderFooter 
 
 Set hdrFirstPage = ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage) 
 
 If hdrFirstPage.Index = wdHeaderFooterFirstPage Then 
 With hdrFirstPage.Shapes.AddShape(Type:=msoShapeHeart, _ 
 Left:=36, Top:=36, Width:=36, Height:=36) 
 .Fill.ForeColor.RGB = RGB(Red:=255, Green:=0, Blue:=0) 
 End With 
 End If 
 
End Sub

IsEmpty


Dim booIsEmpty As Boolean
booIsEmpty = Selection.HeaderFooter.IsEmpty

IsHeader

True if the specified HeaderFooter object is a header.


With ActiveDocument.ActiveWindow.ActivePane.View 
 .Type = wdPrintView 
 .SeekView = wdSeekCurrentPageHeader 
End With 
 
If Selection.HeaderFooter.IsHeader = True Then 
 ActiveDocument.ActiveWindow.ActivePane.View _ 
 .SeekView = wdSeekCurrentPageFooter 
End If 
 
Selection.HeaderFooter.PageNumbers.Add

LinkToPrevious

True if the specified header or footer is linked to the corresponding header or footer in the previous section.

When a header or footer is linked, its contents are the same as in the previous header or footer. Because the LinkToPrevious property is set to True by default, you can add headers, footers, and page numbers to your entire document by working with the headers, footers, and page numbers in the first section. For instance, the following example adds page numbers to the header on all pages in all sections of the active document.


ActiveDocument.Sections(1) _ 
 .Headers(wdHeaderFooterPrimary).PageNumbers.Add

PageNumbers

Returns a PageNumbers collection that represents all the page number fields included in the specified header or footer.

For information about returning a single member of a collection, see Returning an object from a collection.


Set myDoc = Documents.Add 
With myDoc.Sections(1).Footers(wdHeaderFooterPrimary) 
 .PageNumbers.Add PageNumberAlignment := wdAlignPageNumberCenter 
End With

Range

Returns a Range object that represents the portion of a document that is contained within the specified header or footer.


Dim rngRange As Range
Set rngRange = Selection.HeaderFooter.Range

Shapes

Returns a Shapes collection that represents all the Shape objects in a header or footer.

This collection can contain drawings, shapes, pictures, OLE objects, ActiveX controls, text objects, and callouts. For information about returning a single member of a collection, see Returning an object from a collection. The Shapes property, when applied to a document, returns all the Shape objects in the main story of the document, excluding the headers and footers. When applied to a HeaderFooter object, the Shapes property returns all the Shape objects found in all the headers and footers in the document.


MsgBox ActiveDocument.Sections(1). _ 
 Headers(wdHeaderFooterPrimary).Shapes.Count