none
Fields.Update is called twice for primary header/footer fields in VBA macros on Word 2010 RRS feed

  • Question

  • It seems there is an issue with the Update call when used on fields inside of the primary header/footer of a document. During one update call on a single Primary Header or Primary Footer, each of the fields get updated twice. This behavior can best be seen when dealing with an Ask Field. If I run the update manually from inside the document, the fields only update once, this only happens programmatically in VBA macros.

     

    Dim oRange As Range

    For Each oRange In ActiveDocument.StoryRanges

                    oRange.Fields.Update

    Next oRange

     

    This issue seems to have been introduced with Word 2010(Word 2003 and 2007 do not experience this issue). Is there a way around this? We've been unable to suppress the second update thus far.

    Thursday, July 14, 2011 6:19 PM

All replies

  • Hello,

    I cannot reproduce the issue in Word 2010 SP1 32-bit. The second update doesn't occur for me: I just run a VBA macro from the VBA IDE. 


    Regards from Belarus (GMT + 2),

    Andrei Smolin
    Add-in Express Team Leader
    Tuesday, July 19, 2011 7:40 AM
  • My apologies, I should have been more descriptive. For this behavior to exist an ask variable must be present in the header/footer of the first page and an odd page also the following checkboxes must be checked:

    1.       Different first page

    2.       Different odd & even pages

    Thursday, July 21, 2011 12:52 PM
  • Hello,
    I believe this is a problem in Word. I've reproduced the issue in Word 2010 SP1 32-bit.
    The macro below causes triple updates of every header in this document. I run the macro from a VBA project created in an Excel workbook.
    Private Sub UpdateFields()
    Dim WordApp As Word.Application
    Set WordApp = GetObject(, "Word.Application")
    Dim doc As Word.Document
    Set doc = WordApp.ActiveDocument
    Dim s As String
    Dim oRange As Word.Range
    Dim i As Integer
    For i = 1 To 17
      On Error Resume Next
      Set oRange = doc.StoryRanges.Item(i)
      On Error GoTo 0
      If Not oRange Is Nothing Then
        If oRange.Fields.Count > 0 Then
          Debug.Print i, oRange.Fields.Count
          oRange.Fields.Update
        End If
      End If
    Next
    End Sub
    

    There are no entries in Word's "Active Application Add-ins".

    Regards from Belarus (GMT + 2),

    Andrei Smolin
    Add-in Express Team Leader
    Thursday, July 21, 2011 1:51 PM
  • Thank you for your time and effort spent investigating and recreating the issue.

     

    We've been trying to work around this issue since our original post, but have been unsuccessful up to this point. Your confirmation further reinforces what our own research has been indicating - this is a problem with Word that we won't be able to resolve directly, and we'll need to wait for a resolution in Word.

     

    We're going to update our documentation for users relying on Word 2010 and proceed.

     

    Once again, thank you for your assistance. It's greatly appreciated.

    Thursday, July 21, 2011 6:53 PM
  • Hi mbeal,

    I use following code to update all the fields in storyranges

     

    Sub checkUpdate()
    Dim doc As Document
    Set doc = ActiveDocument
    
    Dim sRange As Range
    Dim sField As Field
    
    For Each sRange In doc.StoryRanges
    For Each sField In sRange.Fields
    sField.Update
    Next sField
    Next sRange
    
    End Sub
    
    

     

    The Fields.Update is called twice seems to caused by there are two fields included in the Fields collection

    I hope this helps.

     


    Best Regards, Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, July 26, 2011 12:45 PM
    Moderator
  • Hello Calvin,

    Please open the document (see the link above) in word 2010, set the flag File | Options | Advanced | Show document content | "Show field codes instead of their values". Check that there're four ASK fileds in the document. Then run your code or my code and check how many times the ASKs fields produce a messagebox. For me, this occurs 12 times. The expected value is 4.


    Regards from Belarus (GMT + 2),

    Andrei Smolin
    Add-in Express Team Leader
    Wednesday, August 3, 2011 12:03 PM