Skip to content

Updating Word header and footer fields in documents created from Access Merge

In response to my tip on Updating Word fields in Documents Created from Access Merge in AW 9.11, Peter writes:  Note there is a significant issue in the section mentioned above. The code shown does _not_ update fields in headers or footers. Word doesn’t make it easy to work with those sections; you’ll need to something like the below to update fields in headers and footers (as well as the code you showed for the main body). If there are text boxes (particularly threaded text boxes), they’ll have to be done separately as well (not sure how, I don’t use them in the system that I’ve developed). Note mine is for Unlinking the fields, but it’s the same principle.

With oWordDocument

    ‘All fields in body of document

    .Fields.Unlink

 

    Dim oSec As Word.Section

    For Each oSec In .Sections

        LockFieldsInRange oSec.Headers(wdHeaderFooterPrimary).Range

        LockFieldsInRange oSec.Headers(wdHeaderFooterFirstPage).Range

        LockFieldsInRange oSec.Headers(wdHeaderFooterEvenPages).Range

        LockFieldsInRange oSec.Footers(wdHeaderFooterPrimary).Range

        LockFieldsInRange oSec.Footers(wdHeaderFooterFirstPage).Range

        LockFieldsInRange oSec.Footers(wdHeaderFooterEvenPages).Range

    Next oSec

End With

 

‘Updates and locks all fields in a range.

Private Sub LockFieldsInRange(aRange As Word.Range)

    Dim aField As Word.Field

 

    For Each aField In aRange.Fields

        aField.Update

        aField.Unlink

    Next aField

End Sub

 

Peter is right; you do need to update header and footer info separately, and his code is a lot more efficient than the code I have used previously, which involved opening the sections (one by one), selecting all and updating.

About this author

Office 2024 - all you need to know. Facts & prices for the new Microsoft Office. Do you need it?

Microsoft Office upcoming support end date checklist.