Last active
June 28, 2018 14:21
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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