Reply
 
Thread Tools Display Modes
  #1   Report Post  
FFW
 
Posts: n/a
Default Automatically update TOC on Print &/or Save

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.
  #2   Report Post  
Shauna Kelly
 
Posts: n/a
Default

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


"FFW" wrote in message
...
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.



  #3   Report Post  
FFW
 
Posts: n/a
Default

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.
  #4   Report Post  
Posted to microsoft.public.word.docmanagement
Barbara Barbara is offline
external usenet poster
 
Posts: 87
Default Automatically update TOC on Print &/or Save

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?
--
Barbara Hill


"Shauna Kelly" wrote:

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


"FFW" wrote in message
...
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.




  #5   Report Post  
Richie086 Richie086 is offline
Junior Member
 
Posts: 0
Default

[code]
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

[\code]

When I copy/paste the above code into the Macro editor in Word 2007, the following line of code shows up in red.

Code:
If oStory.StoryType wdMainTextStory Then
it seems as if wdMainTextStory is not being understood by the macro editor. I do not see any other mention of this variable anywhere in the code. When I try to run the macro I get a syntax error on that line of code.. Any ideas?
Reply
Thread Tools
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
print labels only john Microsoft Word Help 2 December 16th 04 07:56 AM
word wont print pg 34 cyndi Microsoft Word Help 1 December 16th 04 04:13 AM
How do I use the PRINT field code to ... Dave Microsoft Word Help 3 December 13th 04 03:06 PM
Text Form Field Ref in Footer Won't Update on Screen StarWine Microsoft Word Help 3 December 6th 04 06:17 PM
How do I get links to auto update in word without being asked eve. Mozz Microsoft Word Help 2 December 5th 04 09:09 PM


All times are GMT +1. The time now is 06:28 PM.

Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"