My MS Word 2007 template has a footer with the filename in it. The user is going to open the template and do a "Save As..." to make their document.
I want the filename shown in the footer to update immediately to the new filename.
Is there an AfterSaveEvent
or something that I can use as a hook to start my VBA script that does the update?
Or is there a much easier way?
To do this, follow these steps: Select the Developer tab, and then select Record Macro in the Code group. In the Record Macro dialog box, type Auto-Exec under Macro name, and then select OK. By default, the macro is saved in the Normal template.
To record a macro, select “Record Macro,” found on the Developer tab. You can give your macro any name that you'd like, as long as there are no spaces in the name. The “Store macro in” dropdown menu gives you the option to save the macro to all future Word documents or only to documents based on your template.
Here's how you can find macros and VBA modules in your document: In Word or Excel, click View > Macro > View Macros. In PowerPoint, click View > Macro.
Just create a macro like this (I believe it works better if included in the Normal.dot)
Sub FileSaveAs()
'
' FileSaveAs Macro
' Saves a copy of the document in a separate file
'
Dialogs(wdDialogFileSaveAs).Show
'returns the name including the .doc extension
ChosenFileNameAndExtension = ActiveDocument.Name 'Or use .FullName
' Your code here
End Sub
It will be triggered whenever the user selects "File Save As"
HTH!
This worked based on @belisarius' answer:
Sub UpdateAll()
Dim oStory As Object
Dim oToc As Object
'Exit if no document is open
If Documents.Count = 0 Then Exit Sub
Application.ScreenUpdating = False
For Each oStory In ActiveDocument.StoryRanges
oStory.Fields.Update 'Update fields in all stories
Next oStory
For Each oToc In ActiveDocument.TablesOfContents
oToc.Update 'Update table of contents
Next oToc
Application.ScreenUpdating = True
End Sub
Sub FileSaveAs()
'
' FileSaveAs Macro
' Saves a copy of the document in a separate file
'
Dialogs(wdDialogFileSaveAs).Show
UpdateAll
End Sub
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With