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

Including Footnotes and Endnotes in Word Counts

When you have Word calculate how many words there are in a document, it normally doesn't pay attention to text in ...

Discover More

Automatically Adding Tabs in Footnotes

When you add a footnote to a document, Word's normal formatting adds a space after the footnote number and before the ...

Discover More

Running Macros Based on Keywords

Wouldn't it be great if Word could execute a macro every time someone typed in a particular keyword or phrase? Word may ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More WordTips (ribbon)

Field Reference to Number of Prior Pages

Fields are used for all sorts of purposes in Word, but typically to provide some sort of dynamic information. This tip ...

Discover More

Creating an AutoText List

The AUTOTEXTLIST field is one of those esoteric fields that you may know nothing about. The cool thing it does is it ...

Discover More

Date Last Edited

You can insert several dynamic dates into your document. One you may want is to add the date when the last edit was ...

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 four less than 7?

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.