How to Create or Update a Word Table of Contents with Open XML
Question details
The user needs to generate and populate a Word Table of Contents (TOC) programmatically using Open XML so that it displays correctly when the document is opened.

- Product
- Microsoft Word / Open XML SDK
- Device & OS
- not provided
- Scenario
- Generating a Word document programmatically with a Table of Contents based on document heading styles.
- Observed behavior
- The dynamically generated Table of Contents remains blank because the word processor does not automatically update the TOC field when the file opens.
Ensure your document headings are formatted using standard built-in Word heading styles (Heading 1, Heading 2, etc.) so the TOC field can correctly identify the structure.
Configure Open XML to Force Field Updates on Open
Modify the document settings within your Open XML code to explicitly instruct the word processor to update all fields when the file is opened.
By default, word processors do not refresh field codes, such as the Table of Contents, to save processing time upon opening a file. You must add the UpdateFields instruction in the DocumentSettingsPart.
Check your Open XML generation code to confirm that text blocks meant for the TOC are styled using standard heading identifiers like 'Heading1' and 'Heading2'.
Ensure the TOC field code is accurately configured in the document body. A standard basic TOC field code looks like 'TOC \o "1-3" \h \z \u'.
In your Open XML SDK code, access the DocumentSettingsPart and add the <w:updateFields w:val="true"/> element. This flags the document to prompt an update upon launch.
Save the file, generate the .docx, and open it. When prompted by the word processor to update document fields, click 'Yes' to populate the TOC.
Manually Update the TOC in Word
If the Open XML automatic update flag fails or is suppressed, you can easily populate the TOC by manually refreshing the document fields.
Easily View and Update Open XML Documents in WPS Writer
WPS Writer provides robust compatibility for Open XML generated .docx files. You can effortlessly handle blank TOC issues by using the intuitive built-in reference tools to update fields with a single click.
- 1. Open Your Document: Launch WPS Writer and open your Open XML generated .docx file.
- 2. Locate the TOC: Scroll to the page containing your generated, blank Table of Contents.
- 3. Update the Table: Navigate to the 'References' tab on the top ribbon and click 'Update TOC', or simply right-click the blank table area and select 'Update Field'.
- 4. Save Your File: Once the headings populate, click 'Save' or press Ctrl + S to permanently save the updated Table of Contents to your document.

Frequently Asked Questions
Why is my Open XML Table of Contents showing up completely blank?
A blank TOC in an Open XML generated document usually occurs because word processing software does not automatically calculate and update field values upon opening a file by default. You need to explicitly configure the w:updateFields element in the document settings to trigger the update.
Do I have to use built-in heading styles for the TOC to work?
Yes, standard TOC field configurations rely directly on built-in heading styles (Heading 1, Heading 2, etc.) to map the document hierarchy. If you use custom styles, you must explicitly map them in the TOC field code instruction.
Is it possible to update the TOC programmatically without user prompts?
Using pure Open XML, you cannot silently update the TOC layout because pagination and rendering engines are handled by the client application (like Word or WPS). To update it silently, you would need to run a background process utilizing the application's interop libraries or a macro (VBA).




