VBA & Macro Problems

Fix a Word Macro That Applies Heading 1 but Omits It from the Table of Contents

Steven ParkerSteven Parker Nov 13, 2026 869 views

Question details

The user needs a Word macro to apply the actual Heading 1 style to marked text so it appears in the table of contents.

Fix a Word Macro That Applies Heading 1 but Omits It from the Table of Contents
Product
Microsoft Word
Device & OS
not provided
Scenario
A macro processes text between HEADING and ENDHEADING markers.
Observed behavior
The text looks like Heading 1, but Word identifies the paragraph as Normal and excludes it from the table of contents.
Before you start

Check that each HEADING and ENDHEADING marker and its heading text are in the same paragraph. The replacement removes the markers and applies Heading 1 to the paragraph.

Solution 1Recommended

Apply Heading 1 with a wildcard Find and Replace macro

Use this macro to replace marked text with the captured heading and assign the actual Heading 1 style.

Formatting text to look like a heading does not necessarily change its paragraph style. This macro assigns Heading 1 during replacement so Word can use it in a style-based table of contents.

1
Open the VBA editor

In Word, open the Developer tab and select Visual Basic. In the editor, choose Insert > Module.

2
Add the macro

Paste this code into the module: Sub ApplyMarkedHeadings() With ActiveDocument.Content.Find .ClearFormatting .Replacement.ClearFormatting .Text = "HEADING(*)ENDHEADING" .Replacement.Text = "\1" .Replacement.Style = wdStyleHeading1 .Forward = True .Wrap = wdFindStop .Format = True .MatchWildcards = True .Execute Replace:=wdReplaceAll End With End Sub

3
Run the macro

Return to Word, choose Developer > Macros, select ApplyMarkedHeadings, and click Run. Check a changed paragraph on the Home tab to confirm that Heading 1 is selected.

4
Update the table of contents

Click the table of contents, choose Update Table, and select Update entire table.

Apply Heading 1 with a wildcard Find and Replace macro
Check the markers: The search uses the literal markers HEADING and ENDHEADING. Adjust the search text if your document uses different markers, and check the results if the markers also appear in ordinary text.
Free Microsoft Office alternative

Work with headings and contents in WPS Writer

WPS Office is a free, lightweight alternative with a familiar interface for editing documents and managing headings. Its Microsoft Word format compatibility helps you move existing documents into WPS Writer; it does not fix a Microsoft Word macro.

Apply heading styles and create a table of contents in WPS WriterOpen and edit Microsoft Word DOC and DOCX documentsUse a familiar interface for a smooth transition from Microsoft OfficeWork with a free, lightweight office suite
QA img-9

Frequently Asked Questions

Why does my heading look correct but not appear in the table of contents?

The paragraph may have visual formatting but still use the Normal style. Click the paragraph and check the Styles gallery on the Home tab; apply Heading 1 if it is not selected.

How do I update the table of contents after applying Heading 1?

Click the table of contents, select Update Table, and choose Update entire table to include newly styled headings.

What does the wildcard replacement do to the markers?

The search HEADING(*)ENDHEADING captures the text between the markers. Replacing it with \1 keeps the captured text and removes both markers.

What if Heading 1 is still missing after I update the table?

Check that the paragraph uses Heading 1, then review the table's settings under References > Table of Contents > Custom Table of Contents. Confirm that Heading 1 is included in the table of contents options.