Word Count for a Portion of a Document

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


2

Bernard can add a NumWords field to his document which returns the number of words in the entire document. He wonders if there is a way to include the number of words in, say, a section or a portion of the document. On the status bar he can see the number of words in whatever text he selects in the document. That is the sort of word count he would like to add to the document.

There is no field that will allow you to include this information dynamically. You could, however, use a macro, along with document variables, to include the desired information—at least when it comes to word count for sections.

Consider the following simple macro:

Sub SetValues()
    Dim J As Integer
    Dim v As Variable
    Dim sVName As String

    With ActiveDocument
        For J = 1 To .Sections.Count
            sVName = "Sec" & Format(J, "0#")
            For Each v In .Variables
                If v.Name = sVName Then v.Delete
            Next v
            .Variables.Add Name:=sVName, _
              Value:=.Sections(J).Range.ComputeStatistics(wdStatisticWords)
        Next J
    End With
End Sub

This macro steps through each section in the document and puts together a name for the document variable that will apply to that section. This name is stored in the sVName variable, and will have a format such as Sec01, Sec02, Sec03, and so on. The code then steps thorough each document variable and, if the name (Sec01, Sec02, etc.) is already in use, then the document variable is deleted. Finally, a new document variable is added, using the defined name, that contains the word count for the section.

Why place the word counts into document variables? Because you can then use a field, in your document, to reference the value (the word count) stored in the document variable. Here is the format of the field if you want to insert the word count for section 2:

{ DOCVARIABLE "Sec02" }

If there is no document variable named "Sec02" that is defined—for instance, you haven't run the macro or the document doesn't have a second section—then the field returns "Error! No document variable supplied." Otherwise, it returns the word count that is stored in the document variable.

Every time you update the text in the various sections of your document, you'll want to re-run the macro to update the word counts. Then, update the fields, and your word counts are updated. Remember, as well, that if you add a new section in the middle of your document, your section numbering will change. Thus, if you previously used a DOCVARIABLE field to get the word count for Sec04, but adding a new section bumped that section to Sec05, you'll need to manually update the DOCVARIABLE field to reference the new section numbering.

If you didn't use sections in your document, and you wanted, instead, to reference portions of the document, you could expand the above concept so that it referred to bookmarked text. It will necessarily be a bit more complex, but will work just as easily. All you need to do is to figure out what naming convention you wanted to use for the bookmarked selections. For instance, let's say you wanted to use bookmarks that used a format such as BkMk01, BkMk02, etc. (See the similarity to the section numbers described earlier?) Now you could run a macro such as this:

Sub SetValues()
    Dim J As Integer
    Dim v As Variable
    Dim sVName As String
    
    With ActiveDocument
        For J = 1 To .Bookmarks.Count
            sVName = .Bookmarks(J).Name
            If Len(sVName) = 6 And Left(sVName, 4) = "BkMk" Then
                For Each v In .Variables
                    If v.Name = sVName Then v.Delete
                Next v
                .Variables.Add Name:=sVName, _
                  Value:=.Bookmarks(J).Range.ComputeStatistics(wdStatisticWords)
            End If
        Next J
    End With
End Sub

This version of the macro is largely the same as previous version, except in how the sVName is determined. In this case, the name of each bookmark is examined, and if the name is exactly 6 characters long and begins with "BkMk", then it is considered a selection of text we should pay attention to because it fits the naming format we decided upon. Other than that, the usage of the field within the actual document is essentially the same:

{ DOCVARIABLE "BkMk04" }

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 (10448) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, Word in Microsoft 365, and 2021.

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

Jumping to Tables

If your document contains quite a few tables, you may find it helpful to jump quickly from one table to another. There ...

Discover More

Searching for Multi-Byte Hex Codes

Need to find a character for which you only know the hex code? There are a few ways you can search for the information, ...

Discover More

Automatically Identifying Repeated Words

Need to find out how many times words are repeated in a document? If so, you'll appreciate the discussion in this tip ...

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)

Inserting the Author Name

Did you know that Word tries to keep track of who the author of a document is? This information can be easily added to ...

Discover More

Updating Fields Automatically

You can place dynamic fields into many parts of your document. If your document contains a lot of fields, you might ...

Discover More

Understanding the COMPARE Field

The COMPARE field is rather esoteric, but it can be helpful when you need to compare two values using fields. The result ...

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 8 - 8?

2022-01-01 14:14:41

David Cohen

Alan has a macro for everything. But really to get a word count on a portion of a Word file, just highlight it and count.


2022-01-01 11:12:54

Bernard Morneau

Mr. Watt,

Thank you for addressing my question and making it the first tip of 2022.

Bernard


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.