Word 2010 Template with Auto-updating fields

Categories: InfoTech
Comments: 2 Comments
Published on: August 8, 2012

It is nice to have consistent looking documents, and Word templates can help you achieve this. For my team, I’d like all our documents to include our company logo and department name at the top of the page. I’d also like the footer to include details about the document:

  • Who last edited the document
  • When the document was last edited
  • Where the document is currently saved – this is useful when you find a useful printed document, and want to print more copies or make an update.

Word includes a feature where you can add fields to a document, including all the above. These fields can be added from the Quick Parts > Fields item from the Insert tab. They are represented by the following field codes:

  • {LASTSAVEDBY}
  • {SAVEDATE}
  • {FILENAME -p} – the -p includes the full path.

So I created a few templates that included these in the footer.

However, Microsoft disabled automatic updating of these fields since Word 2002. According to the MS knowledge base article on this, this was to improve performance. Great, but now the functionality as most people would expect is broken.

So any template that you create which includes any fields will only update those fields if you select them and hit F9 on your keyboard. Who does that? So if you use these fields in a template, they will only ever show the defaults, or at best the details about the template, not the files created from it.

Luckily, Microsoft give a workaround in the knowledge base article in the form of a macro that will automatically run when the document is opened, updating all the fields. This works pretty well, with one small problem.

As soon as the macro runs (when the document is opened), the document is marked as unsaved. So when the user closes the document they will be asked if they want to save their changes. No-one thinks about this prompt do they? They always click Yes, because the last time they didn’t they lost hours of work.

So when the person who was just reading the document clicks Yes, the document is saved. The next time the document is opened the macro runs and updates the fields. They now show who last read the document, and when they closed it.  That isn’t much use.

Happily, there is a document property that implies whether the document has been edited or not.  ActiveDocument.saved.  When this property is true, it means that the document has been saved since the last edit.  When it is false, it means it has not been saved since the last edit.  So by setting ActiveDocument.saved to true after running the field update script, Word no longer sees the need to pop up the save dialog on closing the document.  However, if after the macro has run, the viewer makes a change to the document, ActiveDocument.saved gets set to false and the prompt to save comes back.

I’m pretty pleased with this, so I present to you the updated macro:

Sub AutoOpen()
 '
 ' AutoOpen Macro - runs on open updating 
 ' all the fields in the document.
 '
    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
        ' prompt on close) further changes will set
        ' this back to false restoring the prompt
        ActiveDocument.Saved = True
 
    End If
End Sub

Add this to your template, and now any fields in documents created from this template will be updated when the file opens.

Why not do this when the document is saved

Initially I thought all the fields should be updated on save (as have others). However, the save function would have to run after the document was saved in order to pick up the values of who saved, when and where, and then you would need to save again to update those fields. It makes more sense to update on open, and this will also pick up when the file has been moved.

Thanks to jJack on Stack Overflow for pointing me in the direction of ActiveDocument.saved.

Update

I found that in Word 2010, when the above code was added to Normal.dotm, opening a document in Protected View (e.g. documents downloaded from the Internet or in Outlook attachments) triggered VBscript error 4248: This command is not available because no document is open.

This was happening as the ActiveDocument object wasn’t accessible for protected documents – and it seems that the AutoOpen macro in Normal.dotm runs when any document is opened.  Microsoft have a blog entry on detecting Protected View in macros, which helped me update the above code to not run when a document is opened in Protected Mode.  If you aren’t adding the above macro to Normal.dotm, you don’t need the If statement around the rest of the macro, but it doesn’t hurt either.

2 Comments - Leave a comment
  1. Liz Norman says:

    Wow this is perfect! Just the solution I was looking for. Thanks for posting :)

  2. [...] than saving them Duncan Drury has some nice instructions on how to do this using macros here  http://dunxd.com/2012/08/08/word-2010-template-with-auto-updating-fields Share this:TwitterFacebookGoogleEmailPinterestPocketPrintLike this:Like Loading… This entry was [...]

Leave a comment

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>