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:
Figure 1. The Display options of the Word Options dialog box.
Figure 2. The print settings in the Word Options dialog box.
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.
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. If you would like to see Microsoft add the capability, you can visit their UserVoice forum and vote for such a capability:
https://word.uservoice.com/forums/304924-word-for-windows-desktop-application/suggestions/11685894
Note:
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, and 2016.
Do More in Less Time! Are you ready to harness the full power of Word 2013 to create professional documents? In this comprehensive guide you'll learn the skills and techniques for efficiently building the documents you need for your professional and your personal life. Check out Word 2013 In Depth today!
Fields are a powerful way to add dynamic content to your documents. Some fields rely on the use of parameters to control ...
Discover MoreWord allows you to configure what you see so that field codes are visible instead of the results of those field codes. ...
Discover MoreOnce you save a document on disk, it is stored in a particular folder (or location) on that disk. You may want that ...
Discover MoreFREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
2020-05-21 14:49:58
Ken Johnson
The checkbox identified in Step 6 does not exist in Word 2007 SP3. FYI.
2018-02-27 04:36:12
Slawomir_
Works fine on Word 2013. Thanks!
2017-10-13 10:33:00
Steev
Even CTRL-P, ESC doesn't work in Word 2010 and I do have all the relevant options turned on.
2017-01-26 05:19:01
Anthony Mee
I read your Wordtips "Updating Fields Automatically" (as updated 01 Oct 2016 and available at http://wordribbon.tips.net/T013475_Updating_Fields_Automatically.html) because I have been editing (in Tracked Changes) a report spread across 15 separate sub-sections, and each of which is in a separate Word file. My problem is that the field "filename" in about about 9 or 10 of the sub-sections keeps updating itself before printing, while the remaining sub-sections do not update the Filename.
Are you able to say why this might be happening?
I can email you two 1 page samples if it helps.
2016-10-02 06:21:18
Ken Endacott
The above solutions do not address William's requirement to automatically update the FILENAME field whenever he uses "Save As". This can be done in a macro that replaces the "Save As" command. The following macro should be placed in a user template or in the Normal template.
Sub FileSaveAs()
If Dialogs(wdDialogFileSaveAs).Show Then
Options.UpdateFieldsAtPrint = True
With ActiveDocument
.ActiveWindow.View.Type = wdPrintPreview
.ActiveWindow.Close
.Save
End With
End If
End Sub
2016-10-01 10:16:19
Harold Kingham
As another aid in the print approach, I've added the "Print Preview" icon to the Quick Access Toolbar at the top of the Word window. Then, with one-click, I can view the fields of the document in the preview window, confirm the updated values, and with one more click, be back where I started. Unfortunately, it will not update the TOC, but works well for all other updates.
2016-10-01 08:04:47
Shaun O. Skibinski
Alternatively, you can simply hit [CTRL+A] to select all in the document, then hit [F9]. This will update all fields in the main body of the document, but may not update fields in the header/footer.
2016-10-01 05:12:42
Ken Endacott
Displaying the print dialog box updates all fields except that Table Of Contents page numbers are updated but not the entire TOC. Thus if there have been changes to headings since the TOC was generated the changes will not be included.
If the Print button is subsequently clicked then a dialog box pops up giving the option to update prior to printing just page numbers or the entire TOC. A full update can change the length of the TOC and a repagination can be triggered which for a large document can be time consuming.
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.
Visit the WordTips channel on YouTube
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2022 Sharon Parq Associates, Inc.
Comments