Home > Articles > Home & Office Computing > Microsoft Applications

This chapter is from the book

Updating All Fields Automatically

When you're working with fields, it's common to need to update all of a document's fields at one time. One way to do this is to select the entire document and press F9. This works, but it's a hassle because not only must you perform the extra step of selecting the entire document, but that extra step also means that you lose your current cursor position.

To avoid this problem, use the VBA macro in Listing 3.11 to update all the document's fields.

Listing 3.11. A Macro to Update All the Fields in the Active Document

Sub UpdateAllFields()
    ActiveDocument.Fields.Update
End Sub

Add this macro to a Quick Access toolbar button or assign it a keyboard shortcut (I use Ctrl+Alt+Shift+F9).

It's also often useful to update all of a document's fields when the document is opened. Word doesn't do that by default, but you can create a macro that does.

Launch the Visual Basic Editor and open the project that corresponds to your document. In the project's Microsoft Word Objects branch, double-click the ThisDocument object. In the code window that displays, select Document in the object list and select Open in the event list. Add the following statement to the Document_Open() stub that displays:

ThisDocument.Fields.Update

Figure 3.10 shows the completed code.

Figure 3.10

Figure 3.10 Add this code to your document's Open event to update all fields each time the document is opened.

From Here

  • To learn how to add a custom watermark to a document, see "Creating a Custom Watermark," p. 93.
  • To learn how to protect Excel worksheets, see "Preventing Users from Changing Parts of a Worksheet," p. 148.
  • Excel has a built-in command for creating workspaces; see "Creating a Workspace of Workbooks," p. 153.
  • To learn how to use the Visual Basic Editor and incorporate macros into your documents, see "Working with VBA Macros," p. 429.
  • + Share This
  • 🔖 Save To Your Account