
To update all your fields in a MS Word document is a real pain in the @ss. The header and footer are not updated automatically, and if you use sections, you usually have to update them seperately as well. Or what about having cross references within your headings and a table of contents. Word first updates the table of contents, and only then the cross references in the header. This leaves the Table of Contents to be incomplete.
The macro below should always function. It may be a bit slow (I’m open to suggestions) but it least it will update any document, with any specific construction.
‘
‘ Macro updates all fields within MS Word document. Save this macro in your normal.dot, and attach it to your menu!
‘
‘ Short explanation: This macro truely updates the complete document, including subsections, header, footer, and even if cross links are used in headings.
‘
Sub UpdateAllFields()
‘ All Story Field Updater
On Error GoTo ErrHandler
Dim oStory As Range
Dim TOCCount As Integer
‘ update footers
Dim oSection As Section
Dim oFooter As HeaderFooter
Dim cntField As Integer
Dim actWindow As Window
Dim startPage As Integer
Set actWindow = ActiveDocument.ActiveWindow
startPage = actWindow.Selection.Information(wdActiveEndPageNumber)
Application.StatusBar = “Updating all fields within Document (1/7)”
DoEvents
ActiveDocument.Fields.Update
Application.StatusBar = “Updating all fields within Stories (2/7)”
DoEvents
‘
‘ Update all fields in all Story parts
‘
For Each oStory In ActiveDocument.StoryRanges
Do
cntField = 0
While cntField < oStory.Fields.Count
cntField = cntField + 1
oStory.Fields(cntField).Update
Wend
If (oStory.Fields.Count > 0) Then oStory.Fields.Update
Set oStory = oStory.Next
Loop Until oStory Is Nothing
Next
DoEvents
With actWindow
.Selection.WholeStory
.Selection.Fields.Update
End With
DoEvents
‘
‘ Update all the Table of Contents within the document
‘
Application.StatusBar = “Updating all Table of Contents (3/7)”
DoEvents
TOCCount = ActiveDocument.TablesOfContents.Count
While TOCCount > 0
ActiveDocument.TablesOfContents(TOCCount).Update
TOCCount = TOCCount – 1
Wend
DoEvents
Application.StatusBar = “Updating active panes (4/7)”
DoEvents
‘
‘ Update the active panes: footers in the sections & all fields within the document, including the main index
‘
With actWindow
.ActivePane.View.SeekView = wdSeekMainDocument
.Selection.HomeKey Unit:=wdStory
End With
Application.StatusBar = “Updating headers and footers (5/7)”
DoEvents
‘
‘ Make sure that headers and footers become visible
‘
If actWindow.View.SplitSpecial <> wdPaneNone Then
actWindow.Panes(2).Close
End If
If actWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
ActivePane.View.Type = wdOutlineView Then
actWindow.ActivePane.View.Type = wdPrintView
End If
actWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Application.StatusBar = “Updating sections (6/7)”
DoEvents
For Each oSection In ActiveDocument.Sections
For Each oFooter In oSection.Footers
If oFooter.Exists Then
cntField = 0
While cntField < oFooter.Range.Fields.Count
cntField = cntField + 1
oFooter.Range.Fields(cntField).Update
Wend
End If
Next oFooter
Next oSection
Application.StatusBar = “Updating the document (again) (7/7)”
DoEvents
‘
‘ update the document again, to ensure that if there are cross links within the headers, they are updated as well.
‘
With actWindow
.ActivePane.View.SeekView = wdSeekMainDocument
.Selection.WholeStory
.Selection.Fields.Update
End With
actWindow.Selection.GoTo What:=wdGoToPage, Count:=startPage
Call MsgBox(“Update of all fields is completed.”)
GoTo Einde
ErrHandler:
MsgBox “Error in UpdateAllFieldsn” + Err.Description + “(” + Str(Err.Number) + “)”, vbMsgBoxHelpButton, “Error in UpdateAllFields”, Err.HelpFile, Err.HelpContext
Einde:
End Sub
18 comments
I think this code will do the same thing:
Sub UpdateAllFields()
Dim oStory As Range
Dim oField As Field
For Each oStory In ActiveDocument.StoryRanges
For Each oField In oStory.Fields
oField.Update
Next oField
Next oStory
End Sub
TJ90
Fantastic! Just what I needed.
Thanks
Kevin
amazing very short and soooper and dooper solution …….. u rock
Mubashir Aziz
I just can’t believe I have suffered for so long.
Thank You
Luke
@TJ90 : There sure is room for improvement for the macro.
I am not too sure about your solution, as my question is:
Will it work when I have a field within a header, where the header is used in the table of contents (for example). As the TOC is already updated before the header is…
The macro I use has never failed any combination so far. Therefore I prefer to stick to it, although I realize it is probably quite overdone. Strange though that MS never really fixed this issue.
Richard Zaat
I’m not sure this works 100%, but it worked for me: Ctrl-A F9.
Hans Geurtsen
Thanks Hans, It works every time. Sometimes the simplest way is the better way.
KWM
Problem with Ctrl-A F9 is that it will not update the Header and Footer and some separate sections.
This did work in older versions of MS Word (You could then use the ‘layout view’ and Ctrl-A F9, which would update all), but somehow it no longer works.
Richard Zaat
To update headers & footers, the quick work around is to open the print preview.
Shai ADAR
This almost(!) works… But not all fields are update perfectly, e.g. the table of contents. TJ90’s solutions works better, for me.
Epic
I have reloaded my office. Thanks
sar
Best solution so far from Shai Adar. I had the same problem and this works just fine.
Creative Senses
Select all
Right click anywhere there isn’t a reference and choose “Update Field”
This will update fields, tables of contents, figures, etc. throughout the whole doc.
Doug
Doug,
That would usually work for the document contents, but not the header and the footer. And when you have references containing references (e.g. a reference in a header, which influences the ‘index’).
Regards,
Richard
Richard Zaat
Hi Richard,
Thank you very much. I am also experienced this problem and wondered that Microsoft should include a function like this.
Kind regards
Tan-Hoi
When I run the macro, it does not update fields in the headers of following sections that are cross-referenced to bookmarked text in the first section header. The headers themselves are not linked.
Jim Worthy
Fields in TextBoxes in headers and footers are not updated either
Tbee
Any update to this macro? It doesn’t seem to be working for me with fields in the header in section 2 of the document. The code posted by TJ90 back in ’10 doesn’t work either. Thx.
Richard Belthoff