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

Including Headers and Footers when Selecting All

by Allen Wyatt
(last updated December 2, 2014)

6

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 (522) applies to Microsoft Word 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Word (Word 2007 and later) 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

Comparing Document Versions

Do you need to compare two versions of a document to each other? Word provides a tool that can make this easy, as described ...

Discover More

Deleting Menu Items

Excel allows you to customize your menus so that they contain the commands you want on them. If you later want to delete an ...

Discover More

Naming Tabs for Weeks

Need to set up a workbook that includes a worksheet for each week of the year? Here's a couple of quick macros that can do ...

Discover More

Learning Made Easy! Quickly teach yourself how to format, publish, and share your content using Word 2013. With Step by Step, you set the pace, building and practicing the skills you need, just when you need them! Check out Microsoft Word 2013 Step by Step today!

More WordTips (menu)

Inserting a Cross-Reference to the First Style on a Page

A common way to set up a header is to have it refer to the first occurrence of a heading on the page. (Think how the headers ...

Discover More

Paragraph Numbers in Headers or Footers

If your documents routinely use numbered paragraphs, you may want to place the number of the page's first paragraph in the ...

Discover More

Pulling Headers and Footers from Other Files

You may have some standard headers and footers you want to make available in your document templates. This tip describes some ...

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}] 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 nine more than 8?

2015-08-12 06:35:36

Rod Galvin

Absolute genius, have spent 3 or more days trying to find a solution to updating fields (both in the body and the in the header) of a protected document and the last script works a treat.

I am now about to create the file as a template and hand around to other colleges, hope that it continues to work for all.


Again thank you for solving my problem.


2014-03-18 18:59:09

Your Mom

I feel very certain there is a keyboard short cut to select everything everything...


2013-10-14 13:31:15

Greg Maxey

Actually this entire tips page is misleading and incorrect.

It has nothing to do with Including Headers or Footers when "Selecting All" and might more aptly be titled "Update All Fields."

The first macro will is certainly incomplete and will a) result in a compile error if there is no content in either the header or footer and b) won't update fields in the even pages or first page should those settings be applied.

If you "really want to be complete ..." don't use "MyUpdateFields3" either because it also is incomplete.

Add a shape to a header or footer and then add a field to the shape text range. Run the code. The field will not update.

For a more complete macro, see the demo macro here: http://gregmaxey.mvps.org/word_tip_pages/installing_employing_macros.html


2013-03-20 12:24:45

Chip Orange

I believe the second and third macros need to be combined in order to have one which reliably gets all fields, yet this isn't made clear in the article; is this correct?


2013-01-11 17:56:14

Katherine O

This (the second one for updating all the story layers) works really well, except that it next page section breaks seem to stop when updating the footers.

Thoughts on fixing this?


2012-11-28 11:17:36

DOZE

Many thanks about your last macro which seems to work fine


This Site

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

Newest Tips
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.