How do you exclude tables and captions from the default word count?
Prerequisites
- How to run VBA macro code in MS Word
- How to Install a Micro Button to the Ribbon (Optional)
The Micro
The below VBA macro counts the words in a Microsoft Word document, excluding the following:
- Appendices
- Reference list
- Bibliography
- (Optional) Any specific sections
(Optional) Exclude Any Specific Sections
- Identify the number of sections you have: you can find all the section breaks in the doc by pressing
CMD/Ctrl + F
and searching for^b
. If you press the next/previous occurrence button, it should stop at each section break (the section breaks are invisible, so it may take a couple of times to determine the section numbers accurately). - Update the section index number in the code: for example, I have 12 sections in my document, and I want to exclude the last 4 sections (References, List of Tables, List of Figures & Appendices, I would set
objSection.Index > 8
in the code (please see code below). - I’ve included a debug message at the end of the popup dialogue so you can verify whether you’ve counted the sections correctly.
Sub ExcludeTablesAndCaptionsAndReferenceListsWordsFromWordCount()
Dim objTable As Table
Dim objParagraph As Paragraph
Dim objSection As Section
Dim objDoc As Document
Dim objTableOfContents As TableOfContents
Dim nTableWords As Integer
Dim nDocWords As Integer
Dim nWordCount As Integer
Dim nCaptionWords As Integer
Dim nSectionWordsDiscount As Integer
Dim nTableofContentsWords As Integer
Dim nSectionCount As Integer
Dim objDebugText As String
Set objDoc = ActiveDocument
nTableWords = 0
nCaptionWords = 0
nTableofContentsWords = 0
nSectionWordsDiscount = 0
nDocWords = ActiveDocument.ComputeStatistics(wdStatisticWords)
'Count words from auxiliary sections. E.g., reference lists, bibligraphy, appendices
'Use the format "If objSection.Index > 10 Or objSection.Index = 3 Or objSection.Index = 9 Then"
'to exclude the sections you do not want to be included in the total word count
With objDoc
For Each objSection In .Sections
If objSection.Index > 9 Then
nSectionWordsDiscount = nSectionWordsDiscount + objSection.Range.ComputeStatistics(wdStatisticWords)
End If
Next objSection
End With
'Count words in tables
With objDoc
For Each objTable In .Tables
nTableWords = nTableWords + objTable.Range.ComputeStatistics(wdStatisticWords)
Next objTable
End With
'Count caption words
With objDoc
For Each objParagraph In .Paragraphs
If objParagraph.Style = "Caption" Then
nCaptionWords = nCaptionWords + objParagraph.Range.ComputeStatistics(wdStatisticWords)
End If
Next objParagraph
End With
'Count words in Table of Contents
With objDoc
For Each objTableOfContents In .TablesOfContents
nTableofContentsWords = nTableofContentsWords + objTableOfContents.Range.ComputeStatistics(wdStatisticWords)
Next objTableOfContents
End With
nWordCount = nDocWords - nSectionWordsDiscount - nTableofContentsWords
MsgBox ("There are " & nWordCount & " main text words in this document (" & nWordCount - nTableWords & " excluding words in tables)." _
& vbCr & vbCr & "The following items are excluded from word count: " _
& vbCr & "Total words from references lists, bibliography and appendices: " & nSectionWordsDiscount & "." _
& vbCr & vbCr & "# Additional Info #" _
& vbCr & "Total words in tables: " & nTableWords & "." _
& vbCr & "Table of Contents words: " & nTableofContentsWords & "." _
& vbCr & "Total caption words: " & nCaptionWords & "." _
& vbCr & "There are " & ActiveDocument.Sections.Count & " sections in the document. ")
End Sub