How to use VBA to update fields in a Word document

3 years ago 329

Fields that don't update automatically pose a problem with many users because they don't remember to update them manually. When you need this functionality, use this VBA procedure.

asian-student-surf-internet-for-job-hunting-freelancer-blog-writer-picture-id1132736227.jpg

Image: insjoy, Getty Images/iStockPhoto

The article How to update a page number reference in a Word document uses fields to update cross-references in a Microsoft Word document. The problem is that the fields in use won't update automatically, so the user must remember to do so. That's not ideal because it's a task that's easy to forget. In this article, I'll show you a built-in setting that updates the fields before printing, and then I'll show you a VBA solution.

SEE: 83 Excel tips every user should master (TechRepublic)

I'm using Microsoft 365 on a Windows 10 64-bit system, but you can use earlier versions. Word Online won't support the solutions in this article. For quick access to the VBA code, you can download the demonstration .docm, .doc and .cls files.

How to update when printing in Word

To update an individual field, click on it and press F9 or right-click and select Update Field. To update all references in a document, press Ctrl + A, then press F9. Even if you only have one or a few, it's hard to remember to manually update those fields. Instead, you can set Word to always update fields before printing your document as follows:

  1. Click the File menu and choose Options or More and then Options in the left pane.
  2. Choose Display in the left pane.
  3. In the Printing section, check the Update Fields Before Printing option (Figure A).
  4. Click OK.

Figure A

wordupdatefieldmacro-a.jpg

  Choose this option to update fields when printing.

As you might suspect, this option is limited. First, if you're sending the document by email you won't be printing, so this option is useless. In addition, you might not notice that a field doesn't update if a bookmark or reference is broken because you won't be viewing the document before updating when printing.

This option has its place, but let's look at a code solution that's more dependable.

How to update with VBA in Word

The VBA command to update fields

ThisDocument.Fields.Update

is easy to implement. Listing A shows how easy it is to update all fields when closing a document.

Listing A

Private Sub Document_Close()

'Update all fields when closing document.

    ThisDocument.Fields.Update

End Sub

This procedure uses the Document object's Close event. That means, closing the document triggers the event that updates all fields. It's slick and easy. The only real problem is deciding what event to use, and Close makes the most sense to me. You can use the Open event, but that won't update fields after you modify the document. 

You could trigger both the Open and Close events to cover most situations. In addition, check the option reviewed at the beginning of this article to cover almost every possible scenario:

  • If the user prints the document, the fields are updated first.
  • If the user makes modifications and closes the document before emailing, the fields are updated first.
  • If the user opens the document and prints without making modifications, the fields are updated first.

There is one possibility that isn't covered: The user opens the file, makes modifications, and then sends the file without closing the document, which isn't likely because most of us would close and save the document before sending it.

SEE: Windows 10: Lists of vocal commands for speech recognition and dictation (free PDF) (TechRepublic)

I've added the procedure in Listing A to the downloadable files. I'm using the demonstration files used in the linked article in the introduction. You can work with any Word document, but you'll want to add a field that doesn't update automatically to the body of the document. There are three pages. On page 2 is a text reference. On page 3, a cross-reference field returns the page number of the text reference on page 2. If you enter a page between pages 1 and 2, the text reference moves to page 3 and the cross-reference moves to page 4. However, the cross-reference will still return page 2 because it won't update automatically. 

If you decide to enter the procedure manually, be sure to save the workbook as a macro-enabled file if you're using a ribbon version or the procedure won't run. If you're using a menu version, you can skip this step. To add the procedure, press Alt + F11 to open the Visual Basic Editor. In the Project Explorer to the left, select ThisDocument. You can enter the code manually or import the downloadable .cls file. In addition, the procedure is in the downloadable .docm and .doc files. If you enter the code manually, don't paste from this web page. Instead, copy the code into a text editor and then paste that code into the ThisDocument module. Doing so will remove any phantom web characters that might otherwise cause errors.  

To use the procedure simply close your document. But first, if you're using a demonstration file, add a page to the demonstration .docm or .doc file between pages 1 and page 2 to move the text reference on page 2 to page 3. The cross-reference field that was on page 3 is now on page 4, but it won't update automatically. When prompted to save when you close the file, click Save. When you reopen the document, any field that needs to be updated will display the correct result. To learn more about this demonstration file's fields and its purpose, read the linked article in the introduction.

If you find this a bit too confusing, you don't need to use the demonstration file. You can try this with any Word document that contains fields that don't update automatically.

This simple procedure has one limitation, which doesn't impact our specific example: It updates fields only in the main story of the document. It won't update fields in headers, footers, footnotes or endnotes. That's not an issue for us because Word would update the {PageRef} field (the field we're using) if it were in the header or footer. 

The procedure in Listing A is a good place to start, but as mentioned it has limitations. In a future article, I'll show you how to usurp the Save command to update fields when you save the file. In addition, you can look forward to an article that updates fields beyond the main story. 

Microsoft Weekly Newsletter

Be your company's Microsoft insider by reading these Windows and Office tips, tricks, and cheat sheets. Delivered Mondays and Wednesdays

Sign up today

Also see

Read Entire Article