views:

103

answers:

2

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?

Thanks in advance for any help.

+3  A: 

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!

belisarius
Thanks for the answer. My IT chap's doing some testing on it... will mark this as accepted if it works.
blokeley
@blokeley Please post a comment with the result. Good luck!
belisarius
@belisarius - excellent, it worked first time. I'll post the full solution below for others.
blokeley
+1  A: 

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
blokeley
@blokeley Nice! Thanks for posting it
belisarius