You need to add a footer to a Word document without manually opening the header and footer area each time. VBA macros let you insert, format, and update footers programmatically, saving time when working with many documents or templates. This article explains how to write and run a VBA macro that inserts a footer with text, page numbers, or other fields. You will learn the essential VBA objects and methods for footer manipulation.
Key Takeaways: VBA Footer Insertion in Word
- ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range.Text: Sets the text of the primary footer in the first section.
- wdHeaderFooterPrimary / wdHeaderFooterEvenPages / wdHeaderFooterFirstPage: Constants that target different footer types in a section.
- Fields.Add method with wdFieldPage: Inserts a page number field inside the footer range.
Understanding the VBA Footer Object Model
Word stores footers inside each Section object. A document can have multiple sections, each with up to three footer types: primary (odd pages), even pages, and first page. The Footers collection of a Section object holds these footer ranges. To insert content, you access the Range property of the specific footer constant. The three constants are wdHeaderFooterPrimary, wdHeaderFooterEvenPages, and wdHeaderFooterFirstPage. By default, a new document has one section with a primary footer. You must also ensure the footer area is not linked to the previous section if you work with multiple sections, though for a single-section document this is not needed.
Prerequisites for Running VBA Macros
Before you write a macro, enable the Developer tab in Word. Go to File > Options > Customize Ribbon and check the Developer box in the right panel. You also need to save your document as a macro-enabled file (.docm). Macros cannot run in the default .docx format. If you plan to reuse the macro across documents, store it in the Normal.dotm template.
Steps to Insert a Footer Using VBA
Follow these steps to create a macro that inserts a footer with custom text and a page number.
- Open the VBA Editor
Press Alt+F11 in Word to open the Visual Basic for Applications editor. Alternatively, click Developer > Visual Basic. - Insert a new module
In the Project Explorer pane, right-click Normal or your project name and choose Insert > Module. A blank code window appears. - Write the footer insertion macro
Paste or type the following code into the module:Sub InsertFooter()
Dim sec As Section
Set sec = ActiveDocument.Sections(1)
With sec.Footers(wdHeaderFooterPrimary).Range
.Text = "Page "
.Fields.Add Range:=.Characters(.Characters.Count), Type:=wdFieldPage
.ParagraphFormat.Alignment = wdAlignParagraphCenter
End With
End Sub
This macro sets the primary footer to the word “Page” followed by an automatic page number field, centered. - Run the macro
Place your cursor anywhere inside theInsertFooterprocedure and press F5. Or close the editor and go to Developer > Macros, select InsertFooter, and click Run. - Verify the footer
Open the footer area by double-clicking the bottom margin of any page. You should see “Page 1” centered. The page number updates automatically as you add pages.
Inserting a Multi-Line Footer
To insert a footer with multiple lines, use the vbCrLf constant to add a line break. For example:
- Modify the Range.Text assignment
Change the line.Text = "Page "to.Text = "Company Confidential" & vbCrLf & "Page ". This inserts two lines with a line break between them. - Add the page number field after the second line
The field insertion code remains the same:.Fields.Add Range:=.Characters(.Characters.Count), Type:=wdFieldPage. The field is placed at the end of the footer range.
Targeting Even Pages or the First Page
Documents with different headers and footers for even pages or the first page require you to set the corresponding constants. Enable these options in Word first: Layout > Page Setup > Layout tab > check “Different odd and even” or “Different first page”. Then use these constants in your macro:
- Even pages footer
Usesec.Footers(wdHeaderFooterEvenPages).Rangeto insert content that appears only on even-numbered pages. - First page footer
Usesec.Footers(wdHeaderFooterFirstPage).Rangefor the footer on the first page only. This overrides the primary footer on that page.
Common Issues When Inserting Footers With VBA
“The macro cannot be run because macros are disabled”
Word blocks macros by default for security. Save the document as a .docm file and enable macros when prompted. If the document is already open, go to File > Options > Trust Center > Trust Center Settings > Macro Settings and select “Enable all macros” for testing. Revert to a more secure setting after testing.
Footer text appears but page number field shows “!Unexpected End of Formula”
This error occurs when the field insertion point falls outside the footer range. Ensure you insert the field after setting the text and that the range points to the last character position. Use .Characters(.Characters.Count) to target the exact end of the range.
Footer content duplicates across sections
When a document has multiple sections, the footer of one section may be linked to the previous section. To break the link, set sec.Footers(wdHeaderFooterPrimary).LinkToPrevious = False before modifying the footer. Do this for each section you want to customize independently.
VBA Footer Insertion: Primary vs Even vs First Page
| Item | Primary (wdHeaderFooterPrimary) | Even Pages (wdHeaderFooterEvenPages) | First Page (wdHeaderFooterFirstPage) |
|---|---|---|---|
| Description | Default footer for odd pages | Footer for even-numbered pages | Footer only on the first page |
| Requires setting in Word | None | Layout > Different odd and even | Layout > Different first page |
| Typical use | Page numbers on all pages | Document title on even pages | Disclaimer on first page only |
| VBA constant value | -1 | -3 | -2 |
Understanding these three footer types lets you build macros that match your document layout requirements. The VBA constants are numeric but you should always use the named constants for readability.
You can now insert footers in Word documents using VBA macros with custom text, page numbers, and line breaks. Try combining the Fields.Add method with date fields or file path fields for more dynamic footers. For advanced automation, loop through all sections in a document and apply a consistent footer to each.