none
Why Does Winword Macro Fail to Properly Update Fields in Footnotes the First Time it Runs, But is Okay the Second Time? RRS feed

  • Question

  • I’m working on a Word document with around 140,000 words, 380 pages, and 170 footnotes. The document has cross-reference fields (displaying page numbers) in both the body and the footnotes.

    First thing every morning I run a Word macro that is supposed to (a) update fields in the body, (b) update fields in the footnotes, and (c) perform a few other housekeeping tasks. This macro ran correctly in Word 2010 under Windows 7. However, the macro fails to properly update the fields in the footnotes under up-to-date (non-Insider, subscription) Word 2016 under up-to-date Windows 10 Professional.

    The macro attempts to update the fields in the footnotes because it displays the message “Word can’t undo this action. Do you want to continue?”, which it displays if I manually highlight all the footnotes and select Update Fields. But when I click Yes, the macro adjusts the fields, but the page numbers in the footnote fields are too low, and I see that the page count in the status bar at the bottom of the screen is also too low, although the page count slowly updates to the correct number after the macro stops. This suggests that the document hasn’t been properly paginated so Word is using the wrong page numbers for the fields. In view of this, I’ve added code ActiveDocument.Repaginate in the macro before updating the fields in the footnotes, but that doesn’t solve the problem.

    The odd thing is that if I run the macro a second time, then the fields in the footnotes are properly updated. But if I close the document and reopen it and rerun the macro, then it fails again.

    If I manually update the fields in the footnotes in a newly opened document (i.e., by clicking in the footnotes, entering Ctrl+a, and executing UpdateFields), then Word updates the fields properly.

    Can you suggest an improvement to the macro to ensure that the fields in the footnotes will be properly updated the first time the macro runs?

    Thanks for your help,

    Don Macnaughton

    Saturday, January 28, 2017 9:33 PM

Answers

  • The macro works correctly for me even when running it for the first time - for anything except page references in footnotes/endnotes. Try:

    Sub UpdateAllFields()
    Application.ScreenUpdating = False
    Application.DisplayAlerts = wdAlertsNone
    Dim Rng As Range
    For Each Rng In ActiveDocument.StoryRanges
      Do
        Rng.Fields.Update
        Set Rng = Rng.NextStoryRange
      Loop Until Rng Is Nothing
    Next
    Application.DisplayAlerts = wdAlertsAll
    Application.ScreenUpdating = True
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Monday, January 30, 2017 7:45 AM

All replies

  • Can you suggest an improvement to the macro to ensure that the fields in the footnotes will be properly updated the first time the macro runs?

    That's a bit hard when you haven't actually posted the macro. That said, you might try:

    Sub UpdateFields()
    Application.ScreenUpdating = False
    With ActiveDocument
      .Fields.Update
      .PrintPreview
      .ClosePrintPreview
    End With
    Application.ScreenUpdating = True
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Sunday, January 29, 2017 5:49 AM
  • Paul,

    Thanks for your suggestion. I’ve tested it, and it properly updates the fields in the body of the document. But it doesn’t update the fields in the footnotes. It fails even though the code and the documentation for the Document.Fields property suggests that the Document.Fields collection includes the fields in the footnotes.

    Many years ago I discovered that if I wanted to update the fields in footnotes, I had to move the insertion point into the footnotes, then select all the footnotes, and then execute Selection.Fields.Update.  I use the following code snippet to do that. (My document is always in Print Layout view when I execute the macro.)

    ‘ Switch to normal view.
          ActiveWindow.View.Type = wdNormalView

    ‘ Move the insertion point into footnotes. (The following seven-line If statement was obtained from the macro recorder.)
          If ActiveWindow.ActivePane.View.Type = wdPrintView Or ActiveWindow. _
             ActivePane.View.Type = wdWebView Or ActiveWindow.ActivePane.View.Type = _
             wdPrintPreview Then
                ActiveWindow.View.SeekView = wdSeekFootnotes
          Else
             ActiveWindow.View.SplitSpecial = wdPaneFootnotes
          End If

    ‘ Select all the footnotes.
          Selection.WholeStory

    ‘ Update the fields in the selection.
          If Selection.Fields.Update <> 0 Then
             MsgBox "Field " & ActiveDocument.Fields.Update & " in the update of the footnotes has an error."
             Exit Sub
             End If


    As noted in my original post, this code works the second time I execute the macro after I’ve opened the document, but not the first time. (It worked the first time in Word 2010.) I tried modifying the macro to update the fields in the footnotes twice, thinking that might solve the problem, but that doesn’t work either.

    Does your suggested code properly update the fields in the footnotes in a document on your computer? If so, that would suggest that the problem is local to my computer. If not, that the would suggest that this problem may be due to a small bug in Word.

    Can you think of anything else that I might try?

    Thank you again,

    Don Macnaughton
    Sunday, January 29, 2017 6:20 PM
  • The code I posted works fine in my testing. That suggests you have either a faulty Office installation or a document that has acquired some of corruption.

    You might try repairing the Office installation (via Windows Control Panel > Programs > Programs & Features > Microsoft Office (version) > Change > Repair).

    Corrupt documents can often be 'repaired' by inserting a new, empty, paragraph at the very end, copying everything except that new paragraph to a new document based on the same template (headers & footers may need to be copied separately), closing the old document and saving the new one over it. This won't fix corrupted tables, though, so additional work might be required if the foregoing doesn't resolve the issue. Corrupt tables can often be 'repaired' by:
    • converting the tables to text and back again;
    • cutting & pasting them to another document that you save the document in RTF format, which you then close then re-open before copying them back to the source document; or
    • saving the document in RTF format, closing the document then re-opening it and re-saving in the doc(x) format.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Sunday, January 29, 2017 11:13 PM
  • Hi Don,

    Thanks for visiting our forum.

    Then this is the forum to discuss general questions and feedback for Microsoft word. Since your issue is more related to developing issues involving Word,  I'll move your question to the following MSDN forum for Word:

    https://social.msdn.microsoft.com/Forums/office/en-US/home?forum=worddev

    The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us.

    Thank you for your understanding.

    Best regards,
    Yuki Sun


    Please remember to mark the replies as answers if they helped.

    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Monday, January 30, 2017 2:13 AM
  • Paul,

    Thanks for your sensible suggestions. I’ve done the following:

    1. Do a repair of Office as you suggested. (I did a quick repair.)
    2. Create a brand new document in Print Layout view. (I always work in Print Layout view so I can easily see the footnotes as they’ll appear in the final document.) 
    3. Get 20 paragraphs of plain text Lorem Ipsum text from http://generator.lorem-ipsum.info/_latin
    4. Paste the paragraphs into the blank document several times.
    5. Copy the entire document to the clipboard.
    6. Paste the clipboard into the document enough times to have 330 pages.
    7. Put a new Heading 1 heading on the last page of the document.
    8. Go to the first page and Insert > Cross reference > Heading > Page Number and select the heading and click insert, which causes a cross-reference with the page number to appear at the insertion point on the page.
    9. Copy the cross reference field to the clipboard.
    10. Insert a footnote on the first page of the document.
    11. Put some text in the footnote and then paste the field from the clipboard into the middle of the text in footnote.
    12. Go down two pages and insert two page breaks (with text between them) to move the heading from page 330 to page 332.
    13. IMPORTANT:  Save and close the document.
    14. Reopen the document and give it time to paginate.
    15. Run your macro.


    If I run your macro in Print View the page cross-reference in the body updates correctly to 332, but the page cross-reference in the footnote doesn’t update.

    If I highlight the field in the footnote and execute the Word command UpdateFields, the footnote field updates correctly to 332.

    Curiously, if I run your macro a second time, the cross-reference in the body goes to 3 instead of 332 and the cross reference in the footnote doesn't change.
     
    If I run your macro on a fresh version of the document in Draft view, the field in the body goes to 2 and the field in the footnote doesn't update.

    Do you get the same behavior on your computer.

    I had trouble with the numbering in this post so I hope it displays properly.

    Don



    • Edited by Don Macnaughton Monday, January 30, 2017 3:44 AM minor improvements
    Monday, January 30, 2017 3:39 AM
  • The macro works correctly for me even when running it for the first time - for anything except page references in footnotes/endnotes. Try:

    Sub UpdateAllFields()
    Application.ScreenUpdating = False
    Application.DisplayAlerts = wdAlertsNone
    Dim Rng As Range
    For Each Rng In ActiveDocument.StoryRanges
      Do
        Rng.Fields.Update
        Set Rng = Rng.NextStoryRange
      Loop Until Rng Is Nothing
    Next
    Application.DisplayAlerts = wdAlertsAll
    Application.ScreenUpdating = True
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Monday, January 30, 2017 7:45 AM
  • Paul,

    Thank you for your solution, which works perfectly on my computer. I very much appreciate your help.

    Best regards,

    Don

    Monday, January 30, 2017 5:12 PM