How to Fix Word AutoFormat Errors in Headers and Footers using VBA
Question details
Calling the AutoFormat method directly on a HeaderFooter range in Word VBA causes an object-not-available error.

- Product
- Microsoft Word
- Device & OS
- not provided
- Scenario
- Automating text formatting, such as ordinal numbers, inside headers and footers using VBA or VB.NET.
- Observed behavior
- Word throws an 'object not available' error because the AutoFormat method is not directly supported on HeaderFooter range objects.
Before modifying macro codes, save a backup of your document and familiarize yourself with the VBA Editor to prevent accidental data loss.
Use a Temporary Body Range for AutoFormat
Transfer the header or footer text to a temporary body range, apply the AutoFormat method, and copy the formatted result back.
Because Word does not support running AutoFormat directly inside a HeaderFooter object, you must temporarily transfer the text to the main document body.
When running this process through VB.NET or advanced VBA macros, it is highly recommended to save the user's AutoFormat settings first, enable only the required options (like ordinals), and restore the original settings afterward.
In your VBA editor, set a temporary Range object at the very beginning of the document body using code like 'Set Rng = ActiveDocument.Range(0, 0)'.
Iterate through the Headers and Footers collections for each section in the ActiveDocument.
For each valid header or footer, copy its content using 'Rng.FormattedText = .Range.FormattedText'.
Call 'Rng.AutoFormat' on the temporary body range where the method is natively supported.
Transfer the newly formatted text back to the header or footer using '.Range.FormattedText = Rng.FormattedText'.
Once all headers and footers are processed, clear the temporary range by setting 'Rng.Text = vbNullString'.

Try WPS Office for Seamless Macro and Document Compatibility
If you frequently encounter limitations or complex workarounds with Microsoft Word automation, consider switching to WPS Office. It provides robust compatibility with Word documents and macros, ensuring your automated tasks run smoothly in a lightweight, free environment.
- 1. Download WPS Office: Visit the official WPS website to download and install the free office suite.
- 2. Open your macro-enabled document: Launch WPS Writer and open your .docm or .docx file.
- 3. Enable and run macros: Access the Developer tab to manage, edit, and run your VBA scripts seamlessly.

Frequently Asked Questions
Why does Word AutoFormat fail on headers and footers?
Microsoft Word does not expose the AutoFormat method to the HeaderFooter range object. Calling it directly throws an 'object not available' error, which makes a temporary body range workaround necessary.
How can I AutoFormat only ordinal numbers in VB.NET for Word?
Before calling AutoFormat, save the current Application.Options.AutoFormat settings to variables. Disable all options except for 'ReplaceOrdinals', execute your AutoFormat method on the temporary range, and then restore the original user settings.
Does this workaround affect the original formatting of my document body?
It will not affect the document if you isolate the AutoFormat to a controlled temporary range (like at the very start of the document), apply specific AutoFormat rules, and ensure you clear the temporary text when finished.




