Updating Fields Automatically

Written by Allen Wyatt (last updated April 1, 2023)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, Word in Microsoft 365, and 2021


4

William needs to insert the filename in a table that is in a text box in the footer of a document (it is a client requirement to do it that way). He wants that filename to automatically update every time he saves the document, or at least when he uses "Save As" to create a new file. William has inserted a FILENAME field, but he still has to remember to open the footer and click there and press F9 to update it. He would prefer it to just do it automatically.

Historically, Word was developed with the understanding that your documents would eventually be printed. (This was before the days of doing most tasks online, electronically.) One of the artifacts related to this historical understanding is that Word doesn't update fields until you go to print. Thus, fields are not updated when you do other tasks, such as saving or using Save As.

With this understanding in mind, there are a couple of things you can do. First is to trick Word into thinking you are printing. Before doing this "tricking," however, you'll want to follow these steps:

  1. Display the Word Options dialog box. (In Word 2007 click the Office button and then click Word Options. In Word 2010 and later versions, display the File tab of the ribbon and then click Options.)
  2. At the left of the dialog box click Display. (See Figure 1.)
  3. Figure 1. The Display options of the Word Options dialog box.

  4. Make sure the Update Fields Before Printing check box is selected.
  5. Click Advanced at the left side of the dialog box.
  6. Scroll down until you see the Print section. (See Figure 2.)
  7. Figure 2. The print settings in the Word Options dialog box.

  8. Make sure the Allow Fields Containing Tracked Changes to Update Before Printing check box is selected.
  9. Click on OK to save your changes.

Now, all you need to do when you want to update the fields is to press Ctrl+P. This displays the Print dialog box (Word 2007) or the printing options (later versions of Word). Once you are to this point, the fields in the document should be updated because Word is anticipating that you are going to print. You can press Esc or click the Home tab of the ribbon to abandon printing, and you should note that all the fields in your document are updated. (Well, all the fields except the field used to create a TOC. That's a special case where you actually need to print, not just use Print Preview.)

You could also use a macro to perform these tasks. The following macro doesn't change the settings in the Word Options dialog box, but it does perform just enough of the printing sequence that it tricks Word into updating the fields.

Sub UpdateAllFields()
    With ActiveDocument
        .PrintPreview
        .ClosePrintPreview
    End With
End Sub

There is another macro approach you can take, if desired. This approach bypasses any trickery and, instead, steps through each of the "stories" in a document and updates any fields found in those stories. (A "story" is best viewed as a layer in your document. The main document is one story, headers and footers another, graphics another, and so on.) There are actually two macros used in this approach; you would run the UpdateAllFields macro in order to start the updating process.

Sub UpdateAllFields()
    Dim objStory As Range
    Dim objTOC As TableOfContents
    Dim objTOA As TableOfAuthorities
    Dim objTOF As TableOfFigures
    Dim objIndex As Index

    Application.ScreenUpdating = False
    Application.DisplayAlerts = wdAlertsNone

    For Each objStory In ActiveDocument.StoryRanges
        UpdateFieldsInStory objStory

        While Not (objStory.NextStoryRange Is Nothing)
            Set objStory = objStory.NextStoryRange
            UpdateFieldsInStory objStory
        Wend
    Next
       
    For Each objTOC In ActiveDocument.TablesOfContents
        objTOC.Update
    Next

    For Each objTOA In ActiveDocument.TablesOfAuthorities
        objTOA.Update
    Next

    For Each objTOF In ActiveDocument.TablesOfFigures
        objTOF.Update
    Next

    For Each objIndex In ActiveDocument.Indexes
        objIndex.Update
    Next

    Application.DisplayAlerts = wdAlertsAll
    Application.ScreenUpdating = True
End Sub
Private Sub UpdateFieldsInStory(iobjStory As Range)
    Dim objShape As Shape

    With iobjStory
        .Fields.Update

        Select Case .StoryType
            Case wdMainTextStory, wdPrimaryHeaderStory, _
              wdPrimaryFooterStory, wdEvenPagesHeaderStory, _
              wdEvenPagesFooterStory, wdFirstPageHeaderStory, _
              wdFirstPageFooterStory

                For Each objShape In .ShapeRange
                    With objShape.TextFrame
                        If .HasText Then .TextRange.Fields.Update
                    End With
                Next
        End Select
    End With
End Sub

If you want to find out more information about updating fields using macros, you may want to visit this page at Greg Maxey's site; pay particular attention to the section entitled "Updating Fields":

http://gregmaxey.mvps.org/word_tip_pages/word_fields.html

As you can tell, it is not necessarily easy to update all fields in a document. It would seem that Microsoft could easily add such a capability, but even with many years under the bridge, Word still lacks such a capability.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the WordTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (13475) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, Word in Microsoft 365, and 2021.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Complex Searches for Documents

When working with lots of documents, you may have need from time to time to discover which of those documents contain ...

Discover More

Normal Words Flagged by Spell Check

What do you do when Word's spelling check marks some common, everyday words as wrong? Here are some ideas of places you ...

Discover More

Hyphenating a Selection

Word provides a hyphenation tool that can help you hyphenate words within a document. If you want to apply hyphenation to ...

Discover More

The First and Last Word on Word! Bestselling For Dummies author Dan Gookin puts his usual fun and friendly candor back to work to show you how to navigate Word 2013. Spend more time working and less time trying to figure it all out! Check out Word 2013 For Dummies today!

More WordTips (ribbon)

Updating Fields in Locked Forms

Updating form fields in Word can be confusing, especially when the fields are locked in a form. This tips explains why ...

Discover More

Stopping Fields from Updating when Printing

Fields provide a great way to add dynamic content to your documents. If you don't want those fields to update when you ...

Discover More

Fields Won't Update when Printing

When you print a document, Word normally performs several steps, one of which is to update any fields contained in the ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is eight more than 4?

2024-03-04 20:47:04

Tomek

@Gil:
As I mentioned in my earlier comment, there were times when going to Print Preview did not work. it may still be that way depending on updates you got. In such case I suggest to add a Nul: printer to your system (just add any printer and in its properties specify the port to be "nul:".

The either print to that printer manually, or run the following macro (assumes the name of the printer is "NulPrint":

Sub NulPrint()

CurPrinter = ActivePrinter
ActivePrinter = "NulPrint"
Application.PrintOut
ActivePrinter = CurPrinter

End Sub


2024-03-04 08:42:20

Allen

Gil, it should work provided two things have occurred. First, the document must have been saved. (Word cannot return anything with the FILENAME field unless there is an actual filename, and that happens when you save it.) Second, it must be an actual field code where the outer braces are added either using the ribbon tools or by pressing Ctrl+F9.

-Allen


2024-03-03 23:47:33

Gil bashani

{ FILENAME \p \* MERGEFORMAT }


This footer does not work in word 365
with F9 or with print

Thank you


2023-04-07 00:15:14

Tomek

For a while, doing just Print Preview did not result in updating all fields, at least in my version of Word (365 Family). I am happy to see that this functionality is now back, but if it doesn't work for you, make sure to install any updates available for your version of Word.


This Site

Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.

Videos
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.