Automatically update TOC on Print &/or Save

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I need to be able to update a Table of Contents in a document automatically
when the document is saved and printed. I would prefer to catch both but if
only one is possible its better than nothing. I've used a update all fields
macro but all this does is update the page number. I really need it to
automatically update the entire table. Can anyone tell me if there is an
option I'm mising somewhere or offer some code that could do this?

Thanks in advance.
 
Hi

To ensure an update before printing, do Tools > Options > Print and tick
Update Fields.

To ensure an update before saving requires a macro. You can put a macro in a
document, but that's rarely a good idea, because it will always trigger a
macro warning unless you have macro security set to low, and that setting is
itself not a good idea.

If your document is based on a specific template, then put the following
code in that template. If you do that, then the macro will run whenever you
save documents based on that template.

Alternatively, create a template to do nothing but hold this code and put
that template in your Word startup folder (as shown at Tools > Options >
File Locations > Start Up). The template will load automatically as an
add-in whenever you start Word. If you do this, then the macro will run
whenever you save any document.

Here's the code I routinely use to override the File > Save and File >
SaveAs commands and update all fields before saving:


Sub FileSave()
'
' FileSave Macro
' Saves the active document or template
'

UpdateAll ActiveDocument
ActiveDocument.Save

End Sub
Sub FileSaveAs()
'
' FileSaveAs Macro
' Saves a copy of the document in a separate file
'

Dialogs(wdDialogFileSaveAs).Show
UpdateAll ActiveDocument
ActiveDocument.Save

End Sub


Private Function UpdateAll(oDocument As Document) As Boolean
'Author: Graham Mayor, Microsoft MVP, 2003
'http://www.gmayor.dsl.pipex.com/installing_macro.htm
'Modified by Shauna Kelly, www.ShaunaKelly.com, 2003
Dim oStory As Range
Dim oTOC As TableOfContents

For Each oStory In oDocument.StoryRanges
oStory.Fields.Update
If oStory.StoryType <> wdMainTextStory Then
While Not (oStory.NextStoryRange Is Nothing)
Set oStory = oStory.NextStoryRange
oStory.Fields.Update
Wend
End If
Next oStory

For Each oTOC In ActiveDocument.TablesOfContents
'Oh mystery of life: why does *.Fields.Update not
'always update TOC fields? I don't know.
'But we need to do the TOCs explicitly.
oTOC.Update
Next oTOC

Set oStory = Nothing
Set oTOC = Nothing
End Function




Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
Sorry I wasn't clear from the start - I am indeed working on a template so
that macro works perfectly for me. Thanks a lot for your help.
 
Shauna,

Thanks for this suggestion about how to update all fields when saving a
document. I plan to use it.

Just one little problem: one of the fields I am using is SAVEDATE, in the
main body of the document. (It's also in the footer, but I have no problems
keeping it updated in the footer.) Your code updates and then saves. If I
open a document that was last saved in September, and it is now October, and
I save it again, the field result for SAVEDATE is September when the macro
starts and does the updating, but it is October as soon as the macro is done.

I am tempted to deal with this by changing the macro to save, update, save.
There is still the theoretical problem that the final save is later than the
update, and so the field result during the update is still a little earlier
than the document's actual save date. But since I am interested in getting
the correct month, and not the correct minute, I think I don't care.

Does this make sense to you?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top