3
votes

I have a Word document that uses many different fields. I wrote a macro that updates all the sequence, reference, page, and numpages fields in the document.

Updating text fields reverts them back to their default text so I don't want those updated.

This macro worked perfectly in Word 2007 but I recently updated to Word 2013 and it doesn't work properly anymore.

All page and numpages fields are set to 1 when this macro runs. Yet when I update them manually, they update correctly.

Was there a change to how fields are updated in Office 2013?

The macro code is below.

Sub UpdateAllFields()
UnprotectDocument

'UpdateAllFields Macro
    Dim objDoc As Document
    Dim objFld As Field

'Updates the specified form fields. This can take a while when the document gets large
    Set objDoc = ActiveDocument
    For Each objFld In objDoc.Fields
        If objFld.Type = wdFieldRef Then 'Updates Cross References
            objFld.Update
        If objFld.Type = wdFieldPage Then 'Updates Page Numbers
            objFld.Update
        ElseIf objFld.Type = wdFieldNumPages Then 'Updates Total Page Count
            objFld.Update
        ElseIf objFld.Type = wdFieldSequence Then 'Updates Sequence Fields
            objFld.Update
        End If
    Next objFld

ProtectDocument

End Sub
2
Is it throwing an error? what is UnprotectDocument sub Function?0m3r
your IF THEN ELSE Statement is incomplete0m3r
@omar UnprotectDocument is a function that removes protection from the document in order to allow the macro to edit things outside of the form fields. And yes, the if then else statement wasn't complete. I previously had multiple if statements and thought maybe that was the problem, so I reformatted it as you see above. It's not throwing any errors. The macro runs fine but just updates the page numbers to the wrong value.Mike Lloyd
Furthermore, just using ActiveDocument.Fields.Update also sets all page and numpage references to 1 yet select all + F9 updates them correctly.Mike Lloyd
A coworker and I have a similar problem on Word 2013, but can't reproduce it reliably. Fields update correctly with F9, but Fields(...).Update sets PAGEREFs to 1 instead of the correct page number. His installation behaves differently than mine does, so it may be something in the Registry, which updates are installed, or any of the usual per-machine suspects.cxw

2 Answers

1
votes

It also happened to me that the page references all pointed to page 1 in the document when I used ActiveDocument.Fields.Update, but it worked when I updated them manually. After some trial and error I noticed that it worked using Selection.Fields.Update, so I modified the macro to the following:

Sub UpdateAllFields()
Dim oCurrentRng As Range
Dim oRng As Range

Application.ScreenUpdating = False
Set oCurrentRng = Selection.Range
Set oRng = ActiveDocument.Range
oRng.Select
Selection.Fields.Update

oCurrentRng.Select
Application.Screenupdating = True
End Sub

Hope that it helps someone!

//David

0
votes

You should use SELECT instead of multiple IF ELSE as follow:

Sub UpdateAllFields()

   UnprotectDocument

   'UpdateAllFields Macro
   Dim objDoc As Document
   Dim objFld As Field

   'Updates the specified form fields. 
   'This can take a while when the document gets large
   Set objDoc = ActiveDocument

   For Each objFld In objDoc.Fields

       Select Case objFld.Type

           Case wdFieldRef, wdFieldPage, wdFieldNumPages, wdFieldSequence
               objFld.Update

       End Select

   Next objFld

   ProtectDocument

End Sub

See, code is so clear and easy to understand.