Macro that updates TOC & is triggered by file close

  • Thread starter Thread starter wildetudor
  • Start date Start date
W

wildetudor

I wanted to have my documents' TOC updated everytime I save them, and
therefore I added the following two macros into Normal.dotm:

Sub FileSave()
ActiveDocument.TablesOfContents(1).Update
ActiveDocument.Save
End Sub

Sub FileSaveAs()
Dialogs(wdDialogFileSaveAs).Show
ActiveDocument.TablesOfContents(1).Update
ActiveDocument.Save
End Sub

However, when I save a document as a result of pressing Yes in a file close
dialog ("Do you want to save the changes to...?"), the TOC does not update. I
have therefore found out the code of the FileClose macro and created a new
macro in Normal.dotm with the same and with the same code plus the
ActiveDocument.TablesOfContents(1).Update line that updates the TOC. I now
therefore have a FileClose macro that looks like this:

Sub FileClose()
ActiveDocument.TablesOfContents(1).Update
If Not ActiveDocument.Saved Then
Select Case MsgBox("Do you want to save the changes to " _
& Chr(34) + ActiveDocument.Name + Chr(34) & "?", _
vbExclamation + vbYesNoCancel, "Microsoft Office Word")
Case vbYes
FileSave
Case vbNo
ActiveDocument.Close wdDoNotSaveChanges
Case vbCancel
'Do Nothing
End Select
Else
ActiveDocument.Close wdDoNotSaveChanges
End If
End Sub

However, documents saved from the file close dialog still not have their
TOCs updated. Have I got anything wrong here? Thanks for any help.
 
There are a total of 10 different save commands in Word 2007. You will need
to create macros with the names of a few more of them. Most likely

FilesaveWord11() and FileSaveWordDocx()

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
Back
Top