2

I want to create a Word 2010 template for my team which ensures that every page includes a footer showing the following information:

  • Who last saved the file
  • When the file was last saved
  • The path and filename where the file is stored

So I've created a template file (dotx) with a footer that contains the following field codes:

  • {LASTSAVEDBY *Caps *MERGEFORMAT}
  • {SAVEDATE \@ "d MMM yyy" *MERGEFORMAT}
  • {FILENAME -p}

However when I save this template and create a new document (then save it) the fields are not updated. LASTSAVEDBY shows up as blank, SAVEDATE shows 0 XXX 0000 and FILENAME shows Document1.

I've also tried saving this as a dotm template in case there are some hidden macros, but the results are the same.

0

1 Answer 1

5

So reading Microsoft's KnowledegeBase article on this gives the infuriating answer that this is by design, so you need to write a macro to do this. Handily they provide a Macro which updates the fields when you open the file, rather than save. This actually makes more sense for a number of fields, so as long as you use LASTSAVEDBY and SAVEDATE these will make sense.

Just remember that you won't see the updates applied until you open the document again. For footers this is absolutely fine.

And since this is a Macro you definitely need to save as dotm.

For anyone who needs to update fields on save, there is a discussion at VBA Express forums, or you could just adapt the macro from Microsoft's document.

Update The vbscript from Microsoft is good, but results in a document that always thinks it has been edited, so always prompts to save when closing. Most users will always click on this, which would result in the LASTSAVEDBY and SAVEDATE values showing when the doc was last read, and who by.

So I added a line to immediately mark the document as unchanged after updating the fields. Any further edits to the document will reset this, giving the save prompt on close.

Here's the macro code:

Sub AutoOpen()
'
' AutoOpen Macro
'
'
   Dim aStory As Range
   Dim aField As Field

   For Each aStory In ActiveDocument.StoryRanges

      For Each aField In aStory.Fields
         aField.Update
      Next aField

   Next aStory

 ' set document as unchanged (prevents save dialog popping up when closing)
 ' further edits will set this back to false and restore 
 ' the save dialog on close
 ActiveDocument.Saved = True
End Sub

Update The above code works fine, unless you save this to your Normal.dotm template, in which case it runs everytime you open any document. If the document is opened in Protected View (i.e. it was downloaded from the internet or came as an email attachment) then ActiveDocument isn't available. The solution is to check that the document isn't in Protected View before running the functions inside the macro. Here is the updated macro code, including the If statement to check the doc isn't in Protected Mode:

Sub AutoOpen()
    '
    ' AutoOpen Macro
    '
    '
       Dim aStory As Range
       Dim aField As Field

    ' Check that document is not in Protected View before doing anything
    If Application.ActiveProtectedViewWindow Is Nothing Then

           For Each aStory In ActiveDocument.StoryRanges

              For Each aField In aStory.Fields
                 aField.Update
              Next aField

           Next aStory

         ' set document as unchanged (prevents save dialog popping up when
         'closing) - further changes will set this back
         ActiveDocument.Saved = True
    End If
End Sub

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .