Update Word fields, even in headers and footers

I often use fields in Word documents, for instance for the version of the document.

When comes the time to update all the fields in the document, Internet wisdom tells us to type Control+A, then F9; what else…
Except that it does not completely work: fields that were inserted in headers and footers do not get updated!

The solution? A « macro ». I did not think this day would ever come, but there it is: here is an article about Visual Basic :-P

Cet article est aussi disponible en français.

I tried several macros found on the Internet but none worked for my document; I don’t know why… maybe that was because of the version of Word. The macro that I provide here was made out of many samples found on the Internet; it updates everything: table of contents, table of figures, fields in the document, headers and footers, and even date fields in locked documents, which can be handy sometimes!

Public Sub updateAllFields()
Dim oStory As Object
Dim oToc As Object
Dim oSect As Object
Dim oShape As Shape
Dim i As Integer

' Exit if no document is open
If Documents.Count = 0 Then Exit Sub

Application.ScreenUpdating = False

' Fields in the main body of the document
For Each oStory In ActiveDocument.StoryRanges
oStory.Fields.Update
If oStory.ShapeRange.Count > 0 Then
For Each oShape In oStory.ShapeRange
If oShape.TextFrame.HasText Then
oShape.TextFrame.TextRange.Fields.Update
End If
Next
End If
Next oStory
For Each oToc In ActiveDocument.TablesOfContents
oToc.Update
oToc.UpdatePageNumbers
Next oToc
For Each oToc In ActiveDocument.TablesOfFigures
oToc.Update
oToc.UpdatePageNumbers
Next oToc

i = ActiveDocument.BuiltInDocumentProperties(14) ' number of pages
If i >= 1 Then
For Each oSect In ActiveDocument.Sections
' Headers
oSect.Headers(1).Range.Fields.Update

' Footers
oSect.Footers(1).Range.Fields.Update
Next oSect
End If

Application.ScreenUpdating = True
End Sub

Once the macro has been saved (for example with “View→Macros→View Macros→Create”), you need a practical way of using it whenever you want. The easiest way to achieve that is to put a launcher in the “Quick Access Toolbar”.

In this toolbar, placed to the right of the Word icon in the title bar, click the small trailing arrow; in the menu that opens, choose “More Commands…”; in the drop-down list, choose “Macros”, then select your macro, and click on the “Add” button: the macro shifts from the list on the left to the list on the right. By clicking on your macro, and then on the “Modify…” button, you are given a choice for the icon that best represents the act of updating all fields in the document; there only remains to validate everything!

But, by the way, where are the fields defined?

New versions of Word let you manipulate fields in a location that is variable and impossible to find certainly proven to be optimal for usability. Here is the location that I know:

Depending on the version, you may either open the round Office menu in the title bar, or show the “File” tab in the ribbon (to the left of the “Home” tab). In the properties of the document, choose “Advanced Properties”, which opens a small window. Go to the last tab (“Custom”). Here you are ;-)

Standard fields are in the top list, and custom fields are in the bottom list. The “Name”, “Type” and “Value” input fields that are scattered in the form may be used to either add new custom fields, or edit existing fields from either list.

And how can a field be inserted into the document?

Once again, you have to make do with the weird user interface follow your instinct.

In the “Insert” tab of the ribbon, choose “Quick Parts→Field…”. In the list on the left, towards the two thirds of the list, locate “DocProperty” and click on it: the fields get displayed in the list on the right, where you can select the field you want to insert.

Changelog:

Ajouter un commentaire

Le code HTML est affiché comme du texte et les adresses web sont automatiquement transformées.

La discussion continue ailleurs

URL de rétrolien : http://yalis.fr/cms/index.php/trackback/32

Fil des commentaires de ce billet