Word - Refresh All Fields

In my work, I write a lot of documents. A surprising amount in fact, probably equalled only by the number of spreadsheets. The majority of these (Microsoft Word) documents are produced for clients and I like to ensure as, best I can, that they are not only accurate but well presented. However, in order to simplify document production I make judicious use of templating and Word’s fields functionality. I use not only standard fields but also custom ones as well to update content in multiple places simultaneously on refresh.

Refreshing is as easy as selecting all (CTRl+A) and then pressing my PC’s F9 key … or rather it’s that easy for fields that are in the body of the document. Unfortunately, I also use fields in headers and footers where this update does not work automatically.

But there is a better way…

Automation

My manual method for updating was to update the body document and then open the header, select all again and then F9 again. Repetitive things like this are just crying out to be automated in some way.

Fortunately, Word has its own built-in scripting language, VBA (Visual Basic for Applications) and a good object model from which to trigger various features.

Scripting

The first step is to know that by triggering a print that all of the fields update. Not only the body fields but also all of the header and footer fields. By extension triggering a print preview also does this. Therefore by triggering and then closing a print preview all fields are updated and also the cursor is left undisturbed (unlike when select all is used).

Now that sounds like it does everything we want, so why only ‘the first step’? Well, it’s to do with the order that fields update and that I also use the table of contents field extensively. On occasion, just doing the above will result in the table of contents not actually being up to date and in some cases can even show errors. As a second step, I therefore follow-up with a silent refresh of just the table of contents. If you use other special tabular fields like tables of figures or tables of tables then you might find it useful to extend this script to include updates to those too.

My resulting VBA subroutine looks like this.

Sub RefreshFields()
    'Update all fields (header, footer and body) by forcing a print preview driven refresh
    ActiveDocument.PrintPreview
    ActiveDocument.ClosePrintPreview

    'The table of contents can flag up errors after this so we'll force it to silently refresh too if it exists
    If ActiveDocument.TablesOfContents.Count = 1 Then ActiveDocument.TablesOfContents(1).Update
End Sub

Master Template Update

The next step is to add the script to Word so that it is always available. This is possible via the ‘Normal’ template that Word uses. We need to open and edit this template. Fortunately, Microsoft provides some nice simple instructions on how to open this file for editing.

To add the script you can press ALT+F11 to open the VBA editor and then you need to add a new module. On the left is a project explorer and you should see the Normal template listed. Right click on it, and then select Insert > Module from the context menu. This will create a new module which you can name whatever you wish. I named mine ‘modCustom’. You can then paste the script from above into the new module. That’s the ‘safest’ way, but if you trust this website and are a bit lazier you can choose the Import File option from the context menu instead and then load in my copy of the modCustom.bas file.

Assigning a Shortcut Key

The next step is to assign a keyboard shortcut to the script. This script now shows as a macro in Word so you can run it in a more roundabout way by pressing ALT+F8 and selecting the RefreshFields macro; but this easily earnt a keyboard shortcut for me as I use it so often.

Assigning keyboard shortcuts in Word is not exactly obvious (at least not to me). Microsoft seems to have done their best to obfuscate where it is, but this forum post describes it nicely.

Choose whatever keyboard shortcut works best for you. I wouldn’t recommend overriding the standard F9 shortcut as sometimes you do just want to update fields in a particular selection. For me an alternative involving F9 was preferable, but simply choose something that works for you.

Wrapping Up

To finish off simply close the Normal template and ensure it saves.

To test everything is in place and working close Word, open a document with some fields in the body and header/footer, maybe with a table of contents too. Then make some changes that will affect the fields. This is very easy to do with custom fields, but also pretty easy with some of the standard date fields, adding new headings for tables of contents, etc.

Triggering your keyboard shortcut should briefly (relative to the size/complexity of your document and your PC’s speed) trigger the print preview and then the table of contents update and you should then see all of your fields updated.

I hope you found this useful and that it helps you to speed up and improve your use of Microsoft Word. If you did find it useful, please do reach out and let me know via Twitter where you can find me as [@sylumer](https://twitter.com/sylumer).

Author: Stephen Millard
Tags: | vba | word |

Buy me a coffeeBuy me a coffee



Related posts that you may also like to read