Please Note: This article is written for users of the following Microsoft Word versions: 2007 and 2010. If you are using an earlier version (Word 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Word, click here: Including Headers and Footers when Selecting All.

Including Headers and Footers when Selecting All

Written by Allen Wyatt (last updated September 30, 2022)
This tip applies to Word 2007 and 2010


3

Tim created a simple macro to update all the fields in a template. The macro includes a step that selects the entire document, much like pressing Ctrl+A. This step doesn't select headers and footers, and Tim is afraid that there may be fields to be updated in those locations. He wonders if there is a way to include headers and footers when selecting all, or if there is a different way for his macro to update the fields in the header or footer areas.

The simple answer is that you need to modify your macro code so that it looks in all the different Word areas that there might be fields. The problem with using a "select all" approach is that it only selects text in the main document. Headers, footers, and a host of other elements are maintained in their own separate layers that preclude them from being selected with all the text of the document.

If you simply want to update fields in the main text and in the headers and footers, then you should use a technique such as the following:

Sub MyUpdateFields1()
    ActiveDocument.StoryRanges(wdMainTextStory).Fields.Update
    ActiveDocument.StoryRanges(wdPrimaryFooterStory).Fields.Update
    ActiveDocument.StoryRanges(wdPrimaryHeaderStory).Fields.Update
End Sub

Note that the different layers of a document are referred to as "stories" and maintained within their own collection. While this simple macro very quickly updates the fields in the stories containing the main text, the footers, and the headers, there are other stories (layers) where there could be fields requiring updating. The following macro looks through each story, regardless of its type, and does the requisite updating:

Sub MyUpdateFields2()
    Dim story As Word.Range

    For Each story In ActiveDocument.StoryRanges
        Do
            story.Fields.Update
            ' Check linked stories as linked stories are not independent
            Set story = story.NextStoryRange
        Loop Until (story Is Nothing)
    Next
End Sub

If you really want to be complete, however, there is more than just the different story layers that you need to look through. For instance, it may be that there are some fields contained within text boxes that need updating. A more comprehensive macro is needed to deal with all these other places that fields could be located. In the following example, note the many different elements of the document that can be checked for fields.

Sub MyUpdateFields3()
    Dim doc As Document ' Pointer to Active Document
    Dim wnd As Window ' Pointer to Document's Window
    Dim lngMain As Long ' Main Pane Type Holder
    Dim lngSplit As Long ' Split Type Holder
    Dim lngActPane As Long ' ActivePane Number
    Dim rngStory As Range ' Range Objwct for Looping through Stories
    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 shp As Shape

    ' Set Objects
    Set doc = ActiveDocument
    Set wnd = ActiveDocument.ActiveWindow

    ' get Active Pane Number
    lngActPane = wnd.ActivePane.Index

    ' Hold View Type of Main pane
    lngMain = wnd.Panes(1).View.Type

    ' Hold SplitSpecial
    lngSplit = wnd.View.SplitSpecial

    ' Get Rid of any split
    wnd.View.SplitSpecial = wdPaneNone

    ' Set View to Normal
    wnd.View.Type = wdNormalView

    ' Loop through each story in doc to update
    For Each rngStory In doc.StoryRanges
        If rngStory.StoryType = wdCommentsStory Then
            Application.DisplayAlerts = wdAlertsNone
            ' Update fields
            rngStory.Fields.Update
            Application.DisplayAlerts = wdAlertsAll
        Else
           ' Update fields
           rngStory.Fields.Update
        End If
    Next

    For Each shp In doc.Shapes
        With shp.TextFrame
            If .HasText Then
                shp.TextFrame.TextRange.Fields.Update
            End If
        End With
    Next

    ' Loop through TOC and update
    For Each TOC In doc.TablesOfContents
        TOC.Update
    Next

    ' Loop through TOA and update
    For Each TOA In doc.TablesOfAuthorities
        TOA.Update
    Next

    ' Loop through TOF and update
    For Each TOF In doc.TablesOfFigures
        TOF.Update
    Next

    ' Return Split to original state
    wnd.View.SplitSpecial = lngSplit

    ' Return main pane to original state
    wnd.Panes(1).View.Type = lngMain

    ' Active proper pane
    wnd.Panes(lngActPane).Activate

    ' Close and release all pointers
    Set wnd = Nothing
    Set doc = Nothing
End Sub

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (11489) applies to Microsoft Word 2007 and 2010. You can find a version of this tip for the older menu interface of Word here: Including Headers and Footers when Selecting All.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Defining Styles

Styles are a powerful component of Word. You use them to determine the way that your text should appear. This tip ...

Discover More

Changing the Ribbon's Size and Look

The Ribbon, while debatably handy, can be downright difficult to use for those with a sight impairment. Here are some ...

Discover More

Finding Positions of Formatted Characters in a Cell

With a little bit of work, Excel allows you to format individual characters of the text you place in a cell. If you want ...

Discover More

The First and Last Word on Word! Bestselling For Dummies author Dan Gookin puts his usual fun and friendly candor back to work to show you how to navigate Word 2013. Spend more time working and less time trying to figure it all out! Check out Word 2013 For Dummies today!

More WordTips (ribbon)

Preventing the Left Margin of a Footer from Moving

When you print a document, does the position of the page footer seem to move left and right? This could have to do with ...

Discover More

Including a Printer's Name in a Footer

If you can produce output on a number of different printers, you may want Word to indicate on your printouts which ...

Discover More

Making Wider Footer Margins

Want the margins used in your footers (or headers) to be wider than the margins used in the rest of your document? There ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 1 + 1?

2023-01-17 14:21:00

eric

Hi
Great job !
but footer is not updated ! word 2013 french version
Any idea ?
Thanks


2014-05-22 03:46:43

DOZE

It works fine with Word 2003 but sorry, it doesn't work with Word 2013 or more explicitly, it works only the first time i run it. I'm looking for a new version of "MyUpdateFields3" which could work for 2013. Thanks a lot


2014-01-28 18:25:53

Bruce Bower

Allen,

I have fields throughout my document, in text boxes, in the headers and in the footers. Your macro works for all of these. I have another layer of complication. I have fields in text boxes in the header that the macro does not update. It seems like they are a story within the story of the header. The only way I have been able to update them so far is to get into the header, select the field within the text box and hit F9 to update. Is there a way to update these with the macro?


This Site

Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.

Videos
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.