none
Updating Word tables of contents/figures from a Windows Forms Application RRS feed

  • Question

  • I have a Windows forms application written in Visual Basic which copies a Word document, edits it, updates some custom document properties and then updates fields before saving it.  The editing and setting custom properties are working well, but updating the document fields is not working properly.  Most fields are updated, but the table of contents and tables of figures are not updated. 

    I have tried the following code:

    wordRange = wordDoc.Range()
    wordRange.WholeStory()
    wordRange.Fields.Update()
    wordDoc.PrintPreview()
    wordDoc.ClosePrintPreview()

    and

    wordDoc.Content.Fields.Update()
    wordDoc.PrintPreview()
    wordDoc.ClosePrintPreview()

    and also

    wordRange = wordDoc.Range()
    wordRange.WholeStory()
    For Each field As Word.Field In wordRange.Fields
       resultok = Field.Update()
    Next
    wordDoc.PrintPreview()
    wordDoc.ClosePrintPreview()

    None of these has worked.

    I can go into the document by hand and do a ctrl-A, F9 and I get prompted for updating page numbers only or entire table for each table of contents/figures.  Is there something I need to set to ensure the TOC and TOFs are updated properly?

    I am using Visual Basic 2010 Express and Word 2007.

    Many thanks,

    Tim.


    Wednesday, August 24, 2011 1:55 PM

Answers

  • Hi Tim

    The object model separates updating Tables of Content from other fields. Look at wordDoc.TablesOfContents. to see Intellisense, then try wordDoc.TablesOfContents(1).Update()


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Timarite Thursday, August 25, 2011 11:14 AM
    Wednesday, August 24, 2011 3:32 PM
    Moderator

All replies

  • Hi Tim

    The object model separates updating Tables of Content from other fields. Look at wordDoc.TablesOfContents. to see Intellisense, then try wordDoc.TablesOfContents(1).Update()


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Timarite Thursday, August 25, 2011 11:14 AM
    Wednesday, August 24, 2011 3:32 PM
    Moderator
  • Hi Cindy,

    That is just what I needed.
    Very helpful support, as usual.

    Many thanks,

    Tim.

     

    Thursday, August 25, 2011 11:14 AM
  • Actually, you can update a table of contents via the Update method of the Field class, but it will then default to updating only the page number. For a complete update, using the Update method of TableOfContents is required.

    Stefan Blom, Microsoft Word MVP
    Tuesday, August 30, 2011 3:02 PM