none
ActiveDocument.Fields.Update() doesn't update TOC code RRS feed

  • Question

  • Hi,

    I want to update fields of TOC in my word document for that I am using below code.

    ActiveDocument.Fields.Update();
    ActiveDocument.UndoClear();

    With this code the fields of TOC are not updating. But manually selecting the document  content then pressing F9 will update all fields, including the TOC. I have tried with below code snippet also to update TOC Fields.

    foreach (TableOfContents tableOfContents in ActiveDocument.TablesOfContents) { tableOfContents.Update();

    tableOfContents.UpdatePageNumbers();

    }

    foreach (TableOfFigures tableOfFigures in ActiveDocument.TablesOfFigures) { tableOfFigures.Update(); } foreach (Range storyRange in ActiveDocument.StoryRanges) { storyRange.Fields.Update(); }

    TOC Fields are not updated with the above code snippet. Is there any other ways to update TOC Fields. Can anyone help me on this.

    Thanks,

    Gayatri  

    Monday, August 21, 2017 10:49 AM

All replies

  • Hi Gayatri,

    In my test, TableOfContents.Update works well to update TOC, have you get the ActiveDocument correctly?

    If you use below code to do test, could you see the TOC is selected and updated?

                Word.Application wdApp = Globals.ThisAddIn.Application;

                Word.Document doc = wdApp.ActiveDocument;

                doc.TablesOfContents[1].Range.Select();

                doc.TablesOfContents[1].Update();

    Besides, I suggest you share us your document file with TOC so we could try to reproduce your issue. You could share file with OneDrive and put link here.

    Best Regards,

    Terry

    Tuesday, August 22, 2017 6:41 AM