Fix Word VBA Run-Time Error 91 in a Footer Macro
Question details
The user needs to resolve Run-Time Error 91 in a Word VBA macro designed to modify the footer of a specific section in a complex document.

- Product
- Word
- Device & OS
- not provided
- Scenario
- Running a VBA macro to modify a footer (such as inserting tabs and page numbers) in a specific section of a document that contains multiple sections, like a cover page and a table of contents.
- Observed behavior
- The macro throws Run-Time Error 91 (Object variable or With block variable not set) because the selection-based code fails to accurately locate and set the footer object variable in a heavily sectioned document.
Ensure that your document actually contains the target section (e.g., Section 3) and that the document is not protected against formatting changes before running the macro.
Replace the Selection Object with a Direct Range Reference
Use direct Range referencing instead of the Selection object to target footers reliably and avoid Error 91 in multi-section documents.
Using the Selection object to navigate headers or footers is highly unreliable in complex documents with tables of contents, varied section breaks, and multiple headers. Run-time Error 91 occurs because the macro loses track of the current selection, leaving the object variable unset.
Directly defining a Range object for the exact section and footer guarantees the macro accesses the correct elements without physically moving the cursor, completely resolving the error.
Press Alt + F11 in Word to open the Visual Basic for Applications (VBA) editor.
Find the module containing your footer macro and locate the code relying on the Selection object.
Add 'Dim rng As Range' at the beginning of your procedure to create a dedicated range variable.
Write a With statement targeting the specific footer. For example: 'With ActiveDocument.Sections(3).Footers(wdHeaderFooterPrimary)' to access the primary footer of section 3.
Inside the With block, adjust the text as needed. For example: '.Range.Text = vbTab & vbTab' to insert tabs, then set your range variable with 'Set rng = .Range'.
Add 'rng.Collapse Direction:=wdCollapseEnd' to move to the end of your inserted text, followed by 'ActiveDocument.Fields.Add Range:=rng, Type:=wdFieldPage' to insert the page number.

Run and Edit VBA Macros Seamlessly in WPS Office
WPS Office provides robust support for Visual Basic for Applications (VBA). You can effortlessly edit, debug, and execute your Word macros in WPS Writer to manage complex document headers and footers without compatibility issues.
- 1. Download WPS Office: Download and install the latest version of WPS Office, ensuring you select the edition with built-in VBA support.
- 2. Access Developer Tools: Open your macro-enabled document in WPS Writer, navigate to the 'Developer' tab on the top ribbon, and click 'Macros' or 'Visual Basic'.
- 3. Update and Run Your Code: Paste your corrected Range-based VBA code into the WPS Visual Basic Editor, save, and run the macro to update your footers instantly.

Frequently Asked Questions
What exactly does VBA Run-Time Error 91 mean?
Run-Time Error 91 translates to 'Object variable or With block variable not set.' In Word VBA, this usually happens when you try to manipulate an object (like a footer, a range, or a selection) that hasn't been properly defined or does not exist in the current context of the document.
Why does my footer macro work in a new document but fail in a complex one?
Complex documents with multiple section breaks, cover pages, or tables of contents easily disrupt macros that rely on the Selection object, as the cursor may not land where you expect. A simple document lacks these barriers, which is why poorly targeted code might still run successfully there.
How do I ensure a specific document section exists before my macro runs?
You can add a simple conditional check to your VBA code to verify the section count before executing footer modifications. Adding a line like 'If ActiveDocument.Sections.Count >= 3 Then' ensures section 3 exists, effectively preventing execution errors if the section is missing.




