Word Count for Headers and Footers

by Allen Wyatt
(last updated October 10, 2015)

3

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
            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
        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, 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.

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
            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
        Next h

        For Each f In s.Footers
            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
        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.

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, and 2013.

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

Mass Search and Replace

If you need to change information in dozens or even hundreds of documents, the task can seem insurmountable. Here's a way you ...

Discover More

Sorting Decimal Values

Government and industrial organizations often use a numbering system that relies upon a number both before and after a ...

Discover More

Independent Radio Buttons

Radio buttons are great for some data collection purposes. They may not be that great for some purposes, however, for the ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More WordTips (ribbon)

Including Headers and Footers when Selecting All

If you are creating a macro that, in the course of processing your document, needs to update all the fields in the document, ...

Discover More

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 how ...

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 are ...

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 6 + 0?

2015-10-13 00:11:42

Ken Endacott

Word's word count does not include words in headers/footers whether they are in-line text or in textboxes. The macro in this tip will count in-line words subject to the qualifications about new sections.

The following macros will give counts of words in textboxes in headers and footers. It will not count single special character words such as # or % but will count as a word multiple special characters such as ### or //.

Sub CntHeaderTextboxWords()
Dim hCount As Long
Dim fCount As Long
Dim aShape As Shape
hCount = 0
fCount = 0
For Each aShape In ActiveDocument.Sections(1).Headers(1).Shapes
With aShape.TextFrame
If .HasText Then
Select Case aShape.Anchor.Information(wdHeaderFooterType)
Case 0, 1, 4
hCount = hCount + CntWordsInRange(aShape.TextFrame.TextRange)
Case 2, 3, 5
fCount = fCount + CntWordsInRange(aShape.TextFrame.TextRange)
End Select
End If
End With
Next aShape
MsgBox hCount & " textbox words in headers" & vbCrLf & _
fCount & " textbox words in footers"
End Sub

Function CntWordsInRange(r As Range) As Long
Dim j As Long
Dim i As Long
i = 0
For j = 1 To r.Words.Count
Select Case Trim(r.Words(j))
Case Chr(1) To Chr(47), Chr(58) To Chr(96), Chr(123) To Chr(127)
Case Else
i = i + 1
End Select
Next j
CntWordsInRange = i
End Function

The function CntWordsInRange is useful for counting words in a range of text. For example the following macro will count the words in the body of the document. It is interesting to compare this to the count that Word gives. Which comes down to the question: what is the definition of a word?

Sub WordsInDocBody()
MsgBox CntWordsInRange(ActiveDocument.Range) & " Words in document body"
End Sub


2015-10-12 10:06:37

Jennifer Thomas

I work in a legal firm and we find the Word Count tool useful as well (although it doesn't count the header/footers so this isn't replacing the macro above).

To use Word Count, right-click your status bar and then check Word Count; the word count for the main document layer displays on the status bar as you type.

If you double-click that button, a window displays detailed statistics, and contains a check box that allows you to include words in textboxes, footnotes and endnotes (so with a little subtraction, it's easy to find out what is in those as opposed to the main layer).

Sorry that's a bit off-topic, but it's a welcome tool for us so I hope you find it useful too!


2015-10-11 07:07:12

Ken Endacott

It would be logical to only count the words in headers and footers once no matter how many pages they are repeated on until there is a new section that breaks the 'Link to Previous'. However the macro as it stands increases the word count at each new section even if there is a link to previous. This can be overcome by adding the lines:

If Not h.LinkToPrevious Or s.Index = 1 Then


End If

The macro does not consider text boxes that may be situated in headers and footers. If you want a meaningful count of words you should consider including textboxes. However, the additional code required is quite complex because of the way headers and footers store textboxes


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.

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.