View Single Post
  #10  
Old July 12th, 2004, 01:33 PM
Stefan Blom
external usenet poster
 
Posts: n/a
Default Can't update TOC programmatically (but it worked yesterday)

When you update a table of contents with F9 or by using the context
menu you are prompted either to update only page numbers or to update
the entire table. In VBA, when you reference a table of contents as a
TableOfContents object, this corresponds to calling the
UpdatePageNumbers and Update methods, respectively.

However, if you reference the table of contents as a Field object, you
only have access to the Update method. This means that the coders at
Microsoft had to choose: should the Update method of the Field class
(as well as the Fields collection) update the entire TOC or only the
page numbers? Apparently, they chose the latter.

Two (simple) examples:

Example 1. To update all fields in the main body of the current
document, you can use this code:

ActiveDocument.Fields.Update 'updates all fields
'in main body of document;
'only page numbers updated
'for TOC fields

Example 2. To update all table of contents in the main body of the
current document, you could use something like this:

For Each toc In ActiveDocument.TablesOfContents
toc.Update 'Update entire TOC
Next toc

--
Stefan Blom


"Kathleen" wrote in message
...
Some more info. This is Word 2002 SP-2. I can manually
select the TOC and update it. That's OK.

I can do Select All + F9 to update it. That's OK.

But if I record a macro that does Select All + F9, it
works while I'm recording the macro, but if I play it
back then macro stops working.

The recorded macro looks like this:
Selection.WholeStory
Selection.Fields.Update

Have I accidentally done something bad in Options? I am
forcing the setting for "Update fields when Printing" to
be true in the main macro (that's my workaround for
updating fields in the header)

I'd be very grateful for any insight into this. I need to
release this template and macro set for a group it's just
too flaky to inflict on innoncent people.