logo
search
VBA & Macro Problems

How to Prevent Word VBA Macros from Adding Built-in Headings to a Custom Table of Contents

Partner EditorPartner Editor Sep 30, 2026 868 views

Question details

The user needs a VBA macro to create a custom Table of Contents without including unwanted built-in heading levels.

Product
Word
Device & OS
not provided
Scenario
Recording or writing a VBA macro to generate a custom Table of Contents using only custom styles.
Observed behavior
The recorded macro automatically adds built-in heading levels (generating an \o switch) to the Table of Contents, even when only custom styles are selected during recording.
Before you start

Ensure you have access to the Developer tab and the VBA Editor (accessible by pressing Alt + F11). Familiarity with modifying VBA code parameters is recommended before proceeding.

Solution 1Recommended

Adjust Heading Levels in the TablesOfContents.Add Method

Use specific LowerHeadingLevel and UpperHeadingLevel arguments to force the macro to exclude standard headings.

When Word records a macro for a Table of Contents, it defaults to including standard heading levels 1 through 3 by inserting the \o switch. You can override this behavior by changing the heading parameters in the VBA method.

1
Open the VBA Editor

Press Alt + F11 on your keyboard to launch the Visual Basic for Applications (VBA) Editor.

2
Locate the TOC Macro

Find the specific macro module containing the TablesOfContents.Add code that generates your table.

3
Modify the Heading Level Parameters

Add LowerHeadingLevel:=9, UpperHeadingLevel:=9 to the method's arguments. This forces the generated field switch to become \o "9-9".

4
Run and Test

Execute the macro. Assuming you do not use Heading 9 in your document, this effectively excludes all unwanted lower-level headings from the generated TOC.

Adjust Heading Levels in the TablesOfContents.Add Method
Style Precaution: Ensure that your custom styles are not based on Heading 9, otherwise they may be inadvertently excluded or formatted incorrectly.
WPS Writer

Create Custom Tables of Contents in WPS Writer

Avoid complex VBA macro issues by using WPS Writer's intuitive interface to create and customize your Table of Contents, fully supporting advanced style selections and VBA scripting if needed.

  1. 1. Open WPS Writer: Launch WPS Office and open the document where you want to add a Table of Contents.
  2. 2. Access References: Navigate to the 'References' tab in the top ribbon and click on 'Table of Contents'.
  3. 3. Insert Custom TOC: Select 'Insert Table of Contents' from the dropdown menu to open the comprehensive customization window.
  4. 4. Configure Custom Styles: Click the 'Options' button to manually assign TOC levels to your custom styles, ensuring default headings are unchecked if they are not needed.
Native compatibility with Microsoft Word documents (.docx) and macro-enabled files (.docm)User-friendly dialog to map custom styles to specific TOC levels effortlesslyRobust built-in VBA editor for seamless document automation and maintenance
microsoft office alternative - wps office

Frequently Asked Questions

Why does my recorded Word macro include built-in headings in my custom TOC?

When recording a macro for a Table of Contents, Word automatically generates default switches (such as \o "1-3") in the field code. This switch forces built-in heading levels 1 through 3 to appear in the generated table regardless of your custom style selections during recording.

What does the \o switch in a Table of Contents field code do?

The \o switch instructs the application to build a Table of Contents from paragraphs formatted with built-in heading styles. The numbers immediately following it (for example, "1-3") specify the exact range of heading levels to be included.

Can I manually edit the TOC field code without using VBA?

Yes. You can press Alt + F9 to reveal all field codes in your document. Locate the { TOC } field, manually delete the \o "1-3" switch from the brackets, press Alt + F9 again to hide field codes, and finally press F9 to update the table.