none
Updating fields RRS feed

  • Question

  • My problem is I need to update All fields in Word 2010. I am using the following code.              

    It updates all types of fields in activedocument, but not the Header and Footer sections.

    Any help is appreciated

                    Word.Document activeDoc = Globals.ThisAddIn.Application.ActiveDocument;

                    foreach (Word.Range rngStory in activeDoc.StoryRanges)
                    {
                        foreach (Word.Field fld in activeDoc.Fields)
                        {
                            fld.Update();
                        }
                    }

    Wednesday, September 26, 2012 8:35 PM

Answers

  •                 Word.Document activeDoc = Globals.ThisAddIn.Application.ActiveDocument;

                    foreach (Word.Range myStoryRange in activeDoc.StoryRanges)
                    {
                        myStoryRange.Fields.Update();

                        if (myStoryRange.StoryType == Word.WdStoryType.wdEvenPagesHeaderStory ||
                            myStoryRange.StoryType == Word.WdStoryType.wdPrimaryHeaderStory ||
                            myStoryRange.StoryType == Word.WdStoryType.wdEvenPagesFooterStory ||
                            myStoryRange.StoryType == Word.WdStoryType.wdPrimaryFooterStory ||
                            myStoryRange.StoryType == Word.WdStoryType.wdFirstPageHeaderStory ||
                            myStoryRange.StoryType == Word.WdStoryType.wdFirstPageFooterStory)

                            try
                            {
                                foreach (Word.Shape shape in myStoryRange.ShapeRange)
                                {
                                    shape.TextFrame.TextRange.Fields.Update();
                                }
                            }
                            catch (Exception)
                            {
                            }
                    }
                    // The following routines are necessary because the foregoing updates only page numbers
                    // in TOCs, TOAs and TOFs - field updating doesn't update TOC, TOA or TOF contents.

                    // Loop through Tables Of Contents and update
                    foreach (Word.TableOfContents TOC in activeDoc.TablesOfContents)
                    {
                        TOC.Update();
                    }
                    
                    // Loop through Tables Of Figures and update
                    foreach (Word.TableOfFigures TOF in activeDoc.TablesOfFigures)
                    {
                        TOF.Update();
                    }
                    
                    // Loop through Tables Of Authorities and update
                    foreach (Word.TableOfAuthorities TOA in activeDoc.TablesOfAuthorities)
                    {
                        TOA.Update();
                    }
    • Marked as answer by tootiereoo Thursday, September 27, 2012 3:00 PM
    Thursday, September 27, 2012 3:00 PM

All replies

  • The simplest approach (in vba) is:

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

    If you want to be more selective, or not have the screen flicker produced by the above, you could use:

    Sub RefreshFields()
    Application.ScreenUpdating = False
    Dim TOC As TableOfContents  ' Table of Contents Object
    Dim TOA As TableOfAuthorities ' Table of Authorities Object
    Dim TOF As TableOfFigures ' Table of Figures Object
    Dim Rng As Range ' Word Range Object
    With ActiveDocument
      ' Loop through Story Ranges and update.
      ' Note that this may trigger interactive fields (eg ASK and FILLIN).
      For Each Rng In .StoryRanges
          Rng.Fields.Update
      Next
    ' The following routines are necessary because the foregoing updates only page numbers
    ' in TOCs, TOAs and TOFs - field updating doesn't update TOC, TOA or TOF contents.
      ' Loop through Tables Of Contents and update
      For Each TOC In .TablesOfContents
        TOC.Update
      Next
      ' Loop through Tables Of Authorities and update
      For Each TOA In .TablesOfAuthorities
        TOA.Update
      Next
      ' Loop through Tables Of Figures and update
      For Each TOF In .TablesOfFigures
        TOF.Update
      Next
    End With
    Application.ScreenUpdating = True
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Wednesday, September 26, 2012 9:41 PM
  •                 Word.Document activeDoc = Globals.ThisAddIn.Application.ActiveDocument;

                    foreach (Word.Range myStoryRange in activeDoc.StoryRanges)
                    {
                        myStoryRange.Fields.Update();

                        if (myStoryRange.StoryType == Word.WdStoryType.wdEvenPagesHeaderStory ||
                            myStoryRange.StoryType == Word.WdStoryType.wdPrimaryHeaderStory ||
                            myStoryRange.StoryType == Word.WdStoryType.wdEvenPagesFooterStory ||
                            myStoryRange.StoryType == Word.WdStoryType.wdPrimaryFooterStory ||
                            myStoryRange.StoryType == Word.WdStoryType.wdFirstPageHeaderStory ||
                            myStoryRange.StoryType == Word.WdStoryType.wdFirstPageFooterStory)

                            try
                            {
                                foreach (Word.Shape shape in myStoryRange.ShapeRange)
                                {
                                    shape.TextFrame.TextRange.Fields.Update();
                                }
                            }
                            catch (Exception)
                            {
                            }
                    }
                    // The following routines are necessary because the foregoing updates only page numbers
                    // in TOCs, TOAs and TOFs - field updating doesn't update TOC, TOA or TOF contents.

                    // Loop through Tables Of Contents and update
                    foreach (Word.TableOfContents TOC in activeDoc.TablesOfContents)
                    {
                        TOC.Update();
                    }
                    
                    // Loop through Tables Of Figures and update
                    foreach (Word.TableOfFigures TOF in activeDoc.TablesOfFigures)
                    {
                        TOF.Update();
                    }
                    
                    // Loop through Tables Of Authorities and update
                    foreach (Word.TableOfAuthorities TOA in activeDoc.TablesOfAuthorities)
                    {
                        TOA.Update();
                    }
    • Marked as answer by tootiereoo Thursday, September 27, 2012 3:00 PM
    Thursday, September 27, 2012 3:00 PM