Microsoft Word: Word Count Without Headings

I needed to determine the word count for a Microsoft Word document, omitting all the section titles in the document. I brewed up a little VBA code to do the trick. This was built in Word 2010, but will probably work in the newer versions.


Sub WordCountWithoutHeaders()
' (C) 2013 SpringLight, https://www.springlight.com
' You may use this code in any project you wish as long as you leave the copyright notice in place.
Dim WordCountWhole As Long
Dim WordCountTitles As Long
Dim Titles As String
Dim Msg As String

Application.ScreenUpdating = False
Selection.WholeStory
WordCountWhole = ActiveDocument.ComputeStatistics(wdStatisticWords, False)
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Heading 1")
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
While Selection.Find.Execute()
Titles = Titles & Selection.Text
WordCountTitles = WordCountTitles + Selection.Range.ComputeStatistics(wdStatisticWords)
Wend
Selection.EndKey Unit:=wdStory
Application.ScreenUpdating = True
Msg = "Total Word Count: " & WordCountWhole
Msg = Msg & vbCrLf
Msg = Msg & "Heading1 Word Count: " & WordCountTitles
Msg = Msg & vbCrLf
Msg = Msg & "Text Word Count: " & (WordCountWhole - WordCountTitles)
MsgBox Msg
Debug.Print Titles
Debug.Print Msg
End Sub

In my document the titles are all in the “Heading 1” style, so that’s what this code searches for. If your titles are done in another style, or if your titles are done in multiple styles, you can change one line of code to get the desired result:

Selection.Find.Style = ActiveDocument.Styles(“Heading 1”)

I attached this code to a new button that I placed on the Ribbon, so at the touch of the button the word count is performed. I recommend that.

The code at the end dumps all the titles into the Immediate window in the Visual Basic Editor, which you can copy-and-paste into a new Word document to verify the count, if you wish.

Leave a Reply

Name *
Email *
Website

eleven − one =