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.
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.
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!
If you are a teacher, you may be looking for ways you can use Word's features to correct papers your students send to you ...
Discover MoreWord includes several different tools you can use to improve your writing. One such tool is the translation tool. Here's ...
Discover MoreDo you need to compare two versions of a document to each other? Word provides a tool that can make this easy, as ...
Discover MoreFREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
2024-11-01 10:04:23
Louise
Thank you all for this macro and the updates. I really appreciate this as I'm tracking word counts in the chapters of my PhD thesis and it works perfectly.
I have a table in the document with bookmarks to each of the chapter sections and I'd like to add a "Total" bookmark that shows the sum of all the chapters' word counts. Is this possible? For example, it would show the sum of the wordcounts for chapters 1 to 8?
2023-06-02 08:08:23
TallyWay
Sub InsertWordCount()
Dim oRange As Word.Range
Dim sBookmarkName As String
Dim sTemp As String
sBookmarkName = "WordCount"
With ActiveDocument
sTemp = Format(.Sections(2).Range.ComputeStatistics(wdStatisticWords), "0")
Set oRange = .Bookmarks(sBookmarkName).Range
oRange.Delete
oRange.InsertAfter Text:=sTemp
.Bookmarks.Add Name:=sBookmarkName, Range:=oRange
End With
End Sub
2019-10-14 14:48:57
Michael Tollefson
This old 2007 Knowledge Base article seems still germane: https://support.microsoft.com/en-us/help/291447/word-count-appears-inaccurate-when-you-use-the-vba-words-property. I used your tip, the KB article, and document variables to get what I needed. Thanks.
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.
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.
Visit the WordTips channel on YouTube
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments