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

Setting Row Height

When you enter information into a row on a worksheet, Excel automatically adjusts the height of the row based on what you ...

Discover More

Always Open at 100% Zoom

Tired of shared workbooks opening at some strange zoom factor that makes viewing your data difficult? Here's how to make ...

Discover More

Quick and Dirty Paragraph Count

Need to know how many paragraphs are in a document? You can use Word's Find and Replace feature to get a count quickly.

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 (ribbon)

Inserting the Current Month

Need to add the name of the current month to your document? Word includes a field that can make the addition easy, and it ...

Discover More

Printing Index Field Codes

Word allows you to configure what you see so that field codes are visible instead of the results of those field codes. ...

Discover More

Inserting Custom Properties with Fields

If you define a group of custom properties for a document, you may want a way to display the contents of those properties ...

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 seven more than 1?

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.