How to Prevent Word VBA Macros from Adding Built-in Headings to a Custom Table of Contents
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.
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.
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.
Press Alt + F11 on your keyboard to launch the Visual Basic for Applications (VBA) Editor.
Find the specific macro module containing the TablesOfContents.Add code that generates your table.
Add LowerHeadingLevel:=9, UpperHeadingLevel:=9 to the method's arguments. This forces the generated field switch to become \o "9-9".
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.

Programmatically Remove the Unwanted Switch
Strip the default heading level switch from the generated Table of Contents field code.
Use an AutoText Entry for the TOC Field
Create a reusable AutoText entry containing the correctly configured TOC field code instead of generating it from scratch via VBA.
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. Open WPS Writer: Launch WPS Office and open the document where you want to add a Table of Contents.
- 2. Access References: Navigate to the 'References' tab in the top ribbon and click on 'Table of Contents'.
- 3. Insert Custom TOC: Select 'Insert Table of Contents' from the dropdown menu to open the comprehensive customization window.
- 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.

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.




