Skip to content

Instantly share code, notes, and snippets.

@arthurattwell
Last active June 28, 2018 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arthurattwell/5dc1d445493be266cf31849e90ae15de to your computer and use it in GitHub Desktop.
Save arthurattwell/5dc1d445493be266cf31849e90ae15de to your computer and use it in GitHub Desktop.
Microsoft Office macro for getting the word count of all Word files in a folder
Sub WordcountAll()
' Add the path to the folder you are counting below in quotes where you see C:\
' Adapted from https://www.datanumen.com/blogs/2-quick-ways-count-total-number-pages-words-multiple-word-documents/
Dim objWordApplication As Word.Application
Dim nPageNumber As Long
Dim nWordNumber As Long
Dim objFile As Variant
Dim objFileSystem As Object
Dim objFolders As Object
nPageNumber = 0
Set objWordApplication = New Word.Application
Set objFileSystem = CreateObject("scripting.filesystemobject")
Set objFolders = objFileSystem.getfolder("C:\")
For Each objFile In objFolders.Files
' Only process non-hidden files
If (objFile.Attributes And 2) = 0 Then
With objWordApplication
.Documents.Open (objFile)
nPageNumber = nPageNumber + .ActiveDocument.ComputeStatistics(wdStatisticPages)
nWordNumber = nWordNumber + .ActiveDocument.ComputeStatistics(wdStatisticWords)
.ActiveDocument.Close (False)
End With
End If
Next objFile
MsgBox ("pages:" & nPageNumber & " " & "words:" & nWordNumber)
Set objFile = Nothing
Set objFileSystem = Nothing
Set objFolders = Nothing
Set objWordApplication = Nothing
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment