Skip to content

Instantly share code, notes, and snippets.

@lwkz
Last active November 5, 2023 15:02
Show Gist options
  • Save lwkz/ec1604701fcd559ade744c3953e71f95 to your computer and use it in GitHub Desktop.
Save lwkz/ec1604701fcd559ade744c3953e71f95 to your computer and use it in GitHub Desktop.

This VBA macro counts the words in a Microsoft Word document excluding the following:

  • Appendices
  • Reference list
  • Bibliography

Here is a detailed tutorial on how to run VBA macro code in MS Word: https://www.datanumen.com/blogs/2-methods-exclude-table-texts-word-count-statistics/

If you are adventurous, install a button on the ribbon so it's a button click away.

How to exclude the last few sections (references, bibliography etc.) of the report

  1. Identity the number of sections you have. You can find all the section breaks by pressing CMD/Ctr + F and search 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 fully work out the section numbers).
  2. 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 put objSection.Index > 8.
  3. I've included a debug message at the end of the popup dialog 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

Feel free to comment and ask any questions if anything is unclear and you get stuck.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment