Word Count for Headers and Footers

Written by Allen Wyatt (last updated November 5, 2022)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, 2021, and Word in Microsoft 365


Kate needs to generate a word count for only the headers and footers in a document, and she's at a loss as to how to do so.

There is no automatic way to do it, but you can develop a macro that will figure out the count. VBA allows you to easily step through the headers in each section of your document, and then you can determine how many words are in each header. One approach is to use a macro like the following:

Sub CntHeaderWords()
    Dim s As Section
    Dim h As HeaderFooter
    Dim sRaw As String
    Dim Cnt As Long
    Dim J As Integer

    Cnt = 0
    For Each s In ActiveDocument.Sections
        For Each h In s.Headers
            If Not h.LinkToPrevious Or s.Index = 1 Then
                For J = 1 To h.Range.Words.Count
                    sRaw = h.Range.Words(J)
                    sRaw = Trim(sRaw)
                    If sRaw = vbCrLf Then sRaw = ""
                    If sRaw = vbCr Then sRaw = ""
                    If sRaw = vbLf Then sRaw = ""
                    If Len(sRaw) > 0 Then Cnt = Cnt + 1
                Next J
            End If
        Next h
    Next s

    MsgBox Cnt & " words in headers"
End Sub

When you run this macro, it steps through each section in the document and then each header in that section. The word count is determined for each header and it is added to the Cnt variable. When the macro is complete, it displays the word count for the document's headers.

There are a couple of interesting things to note about this macro. First, notice that the macro checks the LinkToPrevious property for the header. This is done so that in sections beyond the first, linked headers are not processed. Why? Because it makes no sense to add words for each section's header if that header is the same as was in the previous section.

Next, since you can determine a Count property for the Words collection for each header, you might think that you can simply add all those counts together to determine your overall word count. The problem is that even if there is no header for a section, Word returns a word count of 1 for an "implied" header. That's why the macro actually examines each word in the header, and if it is nothing but a carriage return or line feed, then it isn't included in the count.

Another item to note is that if you have any punctuation in your header, each punctuation mark counts as a word. For instance, if you have the header "All rights reserved", Word considers that to be three words. If the header is "All rights reserved." (with the trailing period), then Word considers that to be four words. The upshot is that if you think there may be punctuation in your headers, then you may want to adjust the macro code to not count punctuation.

Finally, you should note that if your header contains objects such as a text box or a table and those objects contain words, then they are not added to the word count. If you develop such documents, you may want to adjust the macro to take those objects into account.

This macro only returns a word count for the headers in a document. If you want, instead, a word count for both headers and footers, then you can adjust the macro in this way:

Sub CntHFWords()
    Dim s As Section
    Dim h As HeaderFooter
    Dim f As HeaderFooter
    Dim sRaw As String
    Dim HdCnt As Long
    Dim FtCnt As Long
    Dim J As Integer

    HdCnt = 0
    FtCnt = 0
    For Each s In ActiveDocument.Sections
        For Each h In s.Headers
            If Not h.LinkToPrevious Or s.Index = 1 Then
                For J = 1 To h.Range.Words.Count
                    sRaw = h.Range.Words(J)
                    sRaw = Trim(sRaw)
                    If sRaw = vbCrLf Then sRaw = ""
                    If sRaw = vbCr Then sRaw = ""
                    If sRaw = vbLf Then sRaw = ""
                    If Len(sRaw) > 0 Then HdCnt = HdCnt + 1
                Next J
            End If
        Next h

        For Each f In s.Footers
            If Not f.LinkToPrevious Or s.Index = 1 Then
                For J = 1 To f.Range.Words.Count
                    sRaw = f.Range.Words(J)
                    sRaw = Trim(sRaw)
                    If sRaw = vbCrLf Then sRaw = ""
                    If sRaw = vbCr Then sRaw = ""
                    If sRaw = vbLf Then sRaw = ""
                    If Len(sRaw) > 0 Then FtCnt = FtCnt + 1
                Next J
            End If
        Next f
    Next s

    sRaw = "Header words: " & HdCnt & vbCrLf
    sRaw = sRaw & "Footer words: " & FtCnt & vbCrLf
    sRaw = sRaw & "Total words: " & HdCnt + FtCnt
    MsgBox sRaw
End Sub

This version of the macro applies the exact same counting technique to each footer as it does to each header, displaying the individual and total counts at the end of the macro.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the WordTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (10285) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, 2021, and Word in Microsoft 365.

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

Flipping a Drawing Object

Don't like the way a drawing object looks? Perhaps flipping the object could help appearances. Excel allows you to flip ...

Discover More

Changing a Workbook Password

Excel allows you to apply protection to your workbooks. If you want to later change the passwords associated with that ...

Discover More

Generating a PDF that Uses CMYK Colors

Getting a Word document into a PDF format that a commercial printer can use can be challenging. This tip examines just ...

Discover More

Discover the Power of Microsoft Office This beginner-friendly guide reveals the expert tips and strategies you need to skyrocket your productivity and use Office 365 like a pro. Mastering software like Word, Excel, and PowerPoint is essential to be more efficient and advance your career. Simple lessons guide you through every step, providing the knowledge you need to get started. Check out Microsoft Office 365 For Beginners today!

More WordTips (ribbon)

Why Does the Footer Expand?

When you add too much text into a footer for that text to display in the space you've allotted to the footer, then Word ...

Discover More

Protecting Headers and Footers

If you don't want the information in a header or footer to be changed by users of your document, there are a couple of ...

Discover More

Inserting the Date in a Header or Footer

Need today's date in the header or footer of your document? Here's how to get it there easily.

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 two more than 3?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


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.