How to refresh Table of Contents?

Mar 13, 2012 at 11:26 AM

I have added a Table of Content to the template3.docx. After all the content controls are updated I would like to have the Table of contents refreshed in order to get the updated values. 

I looked at the charts sample but could not find any objects or classes that directly represent the Table of content type. 

Any idea how I can refresh the table of contents programmatically?

Mar 15, 2012 at 11:25 PM

It's quite complicated to refresh TOC using Open Xml SDK as no straight forward API is there. I can think of few options

1. Using VSTO Add-in to refresh TOC

2. Using Power tools for Open Xml cmdlets

3. Use the method below in Open Xml. This will show a prompt on opening a document. If user accepts TOC will be refreshed

private static void SetDirty(MainDocumentPart mainPart)
            DocumentSettingsPart documentSettingsPart = mainPart.GetPartsOfType<DocumentSettingsPart>().FirstOrDefault();

            if (documentSettingsPart != null)
                documentSettingsPart.Settings.AppendChild(new UpdateFieldsOnOpen() { Val = true });