3

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
0m3r
  • 12,286
  • 15
  • 35
  • 71
Mike Lloyd
  • 31
  • 2
  • Is it throwing an error? what is `UnprotectDocument` sub Function? – 0m3r Jul 17 '15 at 22:27
  • 1
    your `IF THEN ELSE Statement` is incomplete – 0m3r Jul 17 '15 at 22:40
  • @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 Jul 20 '15 at 17:23
  • 1
    Furthermore, just using ActiveDocument.Fields.Update also sets all page and numpage references to 1 yet select all + F9 updates them correctly. – Mike Lloyd Jul 21 '15 at 22:11
  • 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 `PAGEREF`s 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 Jul 04 '16 at 18:23
  • I belive you are getting references pointing to 1 because the recalculation of references (and page numbers) is done before the document is completely loaded/rendered. You can wait for complete render by invoking: ActiveDocument.ComputeStatistics(wdStatisticPages). https://stackoverflow.com/questions/16894257/get-the-number-of-pages-in-a-word-document – Pawel Jasinski Nov 09 '21 at 16:14

2 Answers2

1

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

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.

R.Katnaan
  • 2,486
  • 4
  • 24
  • 36
  • 1
    Thanks. I forgot about the Select function. However, this still doesn't work. I manually updated the page numbers on page 2 of 2 of my document and running this macro reset them to 1 of 1. I also commented out the sub functions to ensure they weren't causing a conflict, and yet the problem persists. It seems that something changed between Word 2007, where I wasn't having problems with this macro, and 2013. – Mike Lloyd Jul 20 '15 at 14:07