Automatically update every field element (e.G., Table of Contents, Document Fields, Computed fields, etc.) in a Microsoft Word Document in one motion.
In the business analysis, tech-lead part of my job, I tend to create Word documents that can be used in a "template" way. For example, if I have revision numbers, dates, company names, document titles, etc., I’ll create document fields and place them in the document instead of the actual value. This way, if I want to recycle the document for a different situation, I only need to modify a few key fields and update the document.
However, Word doesn’t give you a way to update all the fields at once. You have to highlight the entire document and then update the various components separately. This includes visiting each set of headers and footers if you happened to use a field like a document title in them.
So, to make life easier for me, I created this little macro that will visit everything in the document and update it. Here’s the source code:
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
Just copy this snippet into your base, Normal.dot. Then, tie the macro to a button on a command bar and you have a one click "Update All" widget!
The next time you want to change something about a document, all you have to do is choose "File Properties", change the field on the custom tab, return to the document and click your snazzy "Update All" button. Presto, new document with different titles, customer names and numbers! With this macro, you’ll be cranking out documents faster than anyone . . .
thanks for posting this, very handy.
Brilliant, thanks!
very fast and easy, perfect…
Thanks man. You’re a savior.
Thanks man … this is exactly what I was searching for … don’t know why Microsoft didn’t include such functionality, because it is really neccessary.
there is a simpler wau«y of doing that without any macro…just select all document (Ctrl A) and then press F9
That is great, and has worked for me in the past, but doesn’t seem to work on older documents when I’m in compatability mode (footers don’t update), does anyone have any thoughts?
You can also do the following:
* type ctrl-A: this will select entire document
* press F9: this will update all the fields.
Select All + F9 doesn’t select the fields in your headers and footers. If you have multiple sections, you have to visit all the various headers and footers. The above solutions fixes it in one motion.
Hi Curtis.
Perhaps your experience has been different but I found that this code doesn’t seem to completely work with headers/footers in multiple sections. I noticed this after porting some very similar code that I had from Word 2003 to 2007. (Maybe it worked in 2003.) The headers/footers in section 2 of the document didn’t seem to be part of the ActiveDocument.StoryRanges collection, and the DocProperty fields that I had in the footer didn’t get updated.
If it helps anyone, I found that I needed to add the following code to loop through each section and update the fields in headers/footers individually. There are certainly more elegant ways to write this code, but the gist of it is:
Dim section As section
Dim myRange As range
Dim myHF As HeaderFooter
‘Loop through the header/footer for each section.
‘ If this isn’t done specifically, Word seems to
‘ skip the header/footer of sections after section 1.
For Each section In ActiveDocument.Sections
For Each myHF In section.Headers
Set myRange = myHF.range
For Each aField In myRange.Fields
If aField.Type = wdFieldDocProperty Then
aField.Update
End If
Next aField
Next myHF
For Each myHF In section.Footers
Set myRange = myHF.range
For Each aField In myRange.Fields
If aField.Type = wdFieldDocProperty Then
aField.Update
End If
Next aField
Next myHF
Next section
In Word 2003 at least if you switch to Normal view, then select all and F9, it updates all the fields including those in header/footer. Record a quick macro and save it as a button and you accomplish the same as the code.
Mileage may vary with other versions.
DV – brilliant tip about switching to Normal view. Typical MS anomaly! Thanks to everyone.
To update the field codes in the header and footer in Word 2007, Click on the Office Button, Print, Print Preview. You will see then that the field codes in the header and footer have been updated.
+1.
Thanks.
Great tip below–make sure you have it set up to Update fields before printing (Office button, Word Options, Display, Printing Options). Thanks so much!
Anonymous said…
To update the field codes in the header and footer in Word 2007, Click on the Office Button, Print, Print Preview. You will see then that the field codes in the header and footer have been updated.
December 14, 2008 9:39 PM
I must confess I know tool which works ms word and other similar files-docx corrupt repair,program helped my friends and after it helped myself too,it is free as far as I know,utility repairing .docx files and repaire docx document should not be a problem,if you have at least one copy of your *.doc document on corporate file server or somewhere else,tool for repair .docx files allows the user to recover corrupted files in .doc, .docx, .dot and dotx format, as well as *.rtf (Rich Text format),program allows to repair a docx file, repair .docx document, repair .docx damaged files and repair corrupted docx files,it can open your document in Microsoft Word format and attempt to recover any damaged file.
oDoc being my ActiveDocument… these 2 lines worked great for VB6
oDoc.PrintPreview
oDoc.ClosePrintPreview
Thanks a bunch!
Thanks for saving me 2 days of my life!
This is great. It put me on the path.
I actually needed to do this from a VBScript and using your inspiration got me to here:
'====== Start of Script ======
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.Documents.Add("template.dot")
Set objActiveDoc = objWord.ActiveDocument
'….
'.. Document Manipulation Here ..
'….
UpdateAllFields(objActiveDoc)
Sub UpdateAllFields(oActiveDoc)
Dim oStory, oField
For Each oStory In oActiveDoc.StoryRanges
For Each oField In oStory.Fields
oField.Update
Next
Next
End Sub
'====== End of Script ======
thank you
In Word 2007 you can update all field by selecting all document text (Ctrl A) and then pressing F9.
Easy peasy!
Ctrl-A and F9 won't work anymore if parts of your document are protected. So, thanks for the coding
-Chris from Rio
I found that tables of contents were not being updated with this macro and
fields that are within text boxes were not being updated with this macro or with ctrl-a, F9. So I changed the macro as follows:
Sub UpdateAllFields()
Dim oStory As Range
Dim oField As Field
Dim shpShape As Shape
' Update TOCs
ActiveDocument.TablesOfContents(1).Update
If ActiveDocument.TablesOfContents.Count >= 1 Then
For i = 1 To ActiveDocument.TablesOfContents.Count
ActiveDocument.TablesOfContents(i).Update
Next
End If
For Each oStory In ActiveDocument.StoryRanges
For Each oField In oStory.Fields
oField.Update
Next oField
Next oStory
' Update fields in text boxes.
For Each shpShape In ActiveDocument.Shapes
' If the shapetype is a text box.
If shpShape.Type = msoTextBox Then
shpShape.TextFrame.TextRange.Fields.Update
End If
Next shpShape
' User print preview to update headers and footers.
ActiveDocument.PrintPreview
ActiveDocument.ClosePrintPreview
End Sub
A simpler alternative is to click Print – this forces the on-screen display to update (Word is notorious for not updating the display on-screen. It does a good job of keeping track, it just doesn't show it.)
I combined izogi's code with the original code, the result (displayed below) is a true all-in-one update tool which hits all pages, headers, and footers.
Dim Section As Section
Dim MyRange As Range
Dim myHF As HeaderFooter
Dim aField As Field
For Each Section In ActiveDocument.Sections
For Each myHF In Section.Headers
Set MyRange = myHF.Range
For Each aField In MyRange.Fields
aField.Update
Next aField
Next myHF
For Each myHF In Section.Footers
Set MyRange = myHF.Range
For Each aField In MyRange.Fields
If aField.Type = wdFieldDocProperty Then
aField.Update
End If
Next aField
Next myHF
Next Section
For Each MyRange In ActiveDocument.StoryRanges
For Each aField In MyRange.Fields
aField.Update
Next aField
Next MyRange
This is very close to what I am looking for, but still doesn't work. In the header of my document are two textboxes each with fields embedded inside. The above code only deals with situations where the fields are placed directly in the header.
Is there a way to update fields inside textboxes in a header via a script?
awesome, thanks alot!
Thanks a lot for this time saving Macro which updates all fields!
Great information. Thanks for posting.
You are my hero.
I wonder if this would acheive the same effect.
Turn on the automatic update of fields prior to printing a document. By following the instructions below, all one has to do is send the document to the printer, and voila; all fields are updated.
Amazing eh! What will these people at Microsoft think of next? Maybe one day, they'll explain the logic behind why fields are not automatically updated.
Follow these steps in Word 2007:
1. Click the Office button
2. Click Word Options
3. Open the Display section
4. Select Update fields before printing
5. Select Update linked data before printing
6. Click OK
Well yeah, except in usual Microsoft fashion it's buggy and in some case just doesn't update the fields. So I send 80 pages to the printer that I have to scrap, way to go!
Or you can just have it done after "Print Preview' 🙂
Like PaulP mentioned:
If you want a cheap easy way to update ALL fields. Just let word do it for you:
Sub UpdateALL()
ActiveDocument.PrintPreview
ActiveDocument.ClosePrintPreview
End Sub
Seems to be the only way to really update all fields (including textboxes, footers, headers, etc).
Hope this helps =)
thanks so much, this just saved me a lot of time!
To update all defined fields within a word document you can just simply do the following:
DocRef.Fields.Update
Or
ActiveDocument.Fields.Update