Please Note: This article is written for users of the following Microsoft Word versions: 2007 and 2010. If you are using an earlier version (Word 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Word, click here: Word Count for a Section.

Word Count for a Section

by Allen Wyatt
(last updated January 12, 2015)

9

One of the benefits of fields is that you can insert dynamic information within your document. When the field is updated, it is replaced with whatever information is current relative to the field in use. For instance, you can use the NumWords field to insert the number of words in the document. When the field is updated, it is replaced with however many words are then in the document.

If you want to find out the number of words in a section, and have it dynamically placed in a document, then you are out of luck. There is no field that will return this information. You can find it out manually by selecting the text in the section and then choosing the Word Count tool, but that obviously doesn't satisfy the desire to have a value that can be inserted into your document and automatically updated.

This means that you will need to rely on a macro to get the desired word count. If you just want to know the number of words in each section of your document, the following macro can be helpful.

Sub WordCount()
    Dim NumSec As Integer
    Dim S As Integer
    Dim Summary As String

    NumSec = ActiveDocument.Sections.Count
    Summary = "Word Count" & vbCrLf

    For S = 1 To NumSec
        Summary = Summary & "Section " & S & ": " _
          & ActiveDocument.Sections(S).Range.Words.Count _
          & vbCrLf
    Next

    Summary = Summary & "Document: " & _
      ActiveDocument.Range.Words.Count
    MsgBox Summary
End Sub

This simply steps through each section, determines the word count in that section, and displays the summary information in a message box. This does not provide a way to dynamically insert the information in the document, but it does provide an illustration of how you can find the word count of a single section.

A variation on the technique allows you to automatically insert the word count for a specific section at the location of a bookmark within your document. Let's say that you have a bookmarked called "WordCount" that you have defined. This bookmark specifies the place where you want the number of words in the second section of your document. The following macro will determine the word count for the specified section, and then insert the text at the location of the bookmark.

Sub InsertWordCount()
    Dim oRange As Word.Range
    Dim sBookmarkName As String
    Dim sTemp As String

    sBookmarkName = "WordCount"
    With ActiveDocument
        sTemp = Format(.Sections(2).Range.Words.Count, "0")
        Set oRange = .Bookmarks(sBookmarkName).Range
        oRange.Delete
        oRange.InsertAfter Text:=sTemp
        .Bookmarks.Add Name:=sBookmarkName, Range:=oRange
    End With
End Sub

The macro could be easily called from other macros, such as one that runs when the document is opened, saved, or printed. That way the word count would be updated at all the normal times when a field is automatically updated.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (11098) applies to Microsoft Word 2007 and 2010. You can find a version of this tip for the older menu interface of Word here: Word Count for a Section.

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

Goal Seeking

Excel provides a great tool that is helpful in figuring out what certain variables should be in your formulas. This tip ...

Discover More

Dealing with Circular References

Circular references occur when a formula includes a reference to the cell in which the formula appears. Here's how you can ...

Discover More

Using Subentries in an AutoMark File

If you have a large document and need to create a complicated index, an AutoMark file can make your life much easier. This ...

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)

Using Executive Summaries

An executive summary for a longer document may be a nice finishing touch. One of the lesser-known features of Word is that it ...

Discover More

Turning Off Hyphenation for Individual Words

Word does a semi-decent job when it comes to automatically hyphenating your documents. It even lets you exclude certain ...

Discover More

Word Count in Multiple Selections

Getting a word count for an entire document is easy. What you may not know is that some versions of Word can also provide ...

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

2015-10-13 00:32:40

Ken Endacott

The following macros will count the number of words in each section. The count will exclude single special characters but will add a word count where there is more than one special character such as ###. If you compare with Word's word count you will see a significant difference.

Sub SectionWordCount()
Dim j As Long
Dim s As String
s = ""
For j = 1 To ActiveDocument.Sections.Count
s = s & CntWordsInRange(ActiveDocument.Sections(j).Range) & " in Section" & Str(j) & vbCrLf
Next j
MsgBox CntWordsInRange(ActiveDocument.Range) & " words in body of document" & vbCrLf & s
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


2015-09-03 19:35:27

Paul Perez-Jimenez

Has anyone solved the over count issue? I would really like for this macro to work.
thanks


2014-10-22 05:39:45

VICTOR

how do you stop the code from counting blan spaces


2014-07-11 17:47:06

Fred

The problem with the second macro is that the Count property of the Words object includes punctuation marks and paragraph marks as separate words.
You can get rid of the paragraph marks by using "sTemp = Format(.Sections(2).Range.Words.Count - .Sections(2).Range.Paragraphs.Count, "0")" But getting rid of the punctuation would be more difficult.


2014-05-31 07:18:15

Mosh48

Great tip, but can anyone please advise on what field should be used in the first macro to include footnotes.


2013-05-04 02:55:17

Dariusz

Ameet, I misread your mail. Your approach does indeed fix the problem. Thanks!


2013-05-04 02:47:50

Dariusz

Matt, I have the same problem. It appears as though the Count method counts punctuation characters as words.
Does anyone have any tips how to handle this?


2012-09-05 11:23:28

Ameet

Hi Matthew,
Try changing the field to:
sTemp=Format(.Sections(2).Range.ComputeStatistics( wdStatisticWords), "0")


2012-04-11 05:39:12

Matthew

This macro is neat, but the counts are quite inaccurate for the document I tried it on, in Word for Mac 2011. The toolbar shows a total word count of 2850, but ActiveDocument.Range.Words.Count gives 4051. The per-section counts are also about 25% over. I don't know why! I tried converting field codes to plain text, made no difference.


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.