How Can I Get a Total Page Count for All the Word Documents in a Folder?

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I get a total page count for all the Word documents in a folder?

— RA

SpacerHey, Scripting Guy! AnswerScript Center

Hey, RA. You’ll have to forgive the Scripting Guy who writes this column if he seems a little tired this morning. Yesterday was the first day of school in the Lake Washington School District and, as you might expect, the scene in the Scripting Household was one of utter chaos: the kicking and screaming, the tears and tantrums, the sudden onset of beriberi, which made it impossible to leave the house. All in all, it made for a very long and very tiring morning.

Of course, now that we think about it, all the screaming and crying seemed to emanate from the Scripting Dad, who didn’t want to go to work. The Scripting Son calmly got out of bed and went to school, like he always does.

Despite the tears, and despite the beriberi, the Scripting Dad knew he had no choice but to come in to work. After all, the world (or at least you, RA) was depending on him; someone had to come up with a script that could give you a total page count for all the Word documents in a folder:

Const wdStatisticPages = 2

strComputer = “.”

Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)

Set colFiles = objWMIService.ExecQuery _
(“ASSOCIATORS OF {Win32_Directory.Name=’C:\Scripts’} Where ” _
& “ResultClass = CIM_DataFile”)

Set objWord = CreateObject(“Word.Application”)
objWord.Visible = True

For Each objFile in colFiles
If objFile.Extension = “doc” Then
Set objDoc = objWord.Documents.Open(objFile.Name)
intPages = intPages + objDoc.ComputeStatistics(wdStatisticPages)
objDoc.Saved = True
End If

Wscript.Echo “Total pages: ” & intPages

Note. Does the Scripting Guy who writes this column really have beriberi? Well, probably not. On the other hand, take a look at some of the symptoms of beriberi:



Poor mental concentration

Quick, someone call a doctor right away!

OK; now that the script is written let’s see if we can figure out how it works. To begin with, we define a constant named wdStatisticPages and set the value to 2; we’ll use this constant to retrieve the number of pages in each Word document we find.

Note. Are there other statistics we can retrieve for a Word document? You bet there are; for more information, see one of our most-beloved Office Space columns.

After defining the constant we connect to the WMI service on the local computer and then use this line of code to retrieve a collection of all the files found in the folder C:\Scripts:

Set colFiles = objWMIService.ExecQuery _
(“ASSOCIATORS OF {Win32_Directory.Name=’C:\Scripts’} Where ” _
& “ResultClass = CIM_DataFile”)

Next we – what’s that? Can you perform this same task against a remote computer? As a matter of fact you can, provided that Microsoft Word is installed on that remote computer. In order to pull off that trick you need to do two things. First, you need to assign the name of that remote machine to the variable strComputer; for example:

strComputer = “atl-fs-01”

That enables us to connect to the WMI service on the remote computer.

Second, you need to create an instance of the Word.Application object on the remote machine. In the sample script we’re showing you today we’re working with Word documents on the local computer; therefore we use this line of code to create an instance of Word:

Set objWord = CreateObject(“Word.Application”)

However, if you want to work with Word documents on a remote computer you need to include the computer name as the optional second parameter to the CreateObject method:

Set objWord = CreateObject(“Word.Application”, “atl-ws-01”)

Got that? Good.

OK, so where were we? Oh, yeah: we create an instance of the Word.Application object (in this case, an instance of Word on the local computer) and then set the Visible property to True. That, by the way, is optional: we don’t need Word to be visible onscreen in order to add up the number of pages in the various Word documents. However, this does give a visual cue that something is going on. Leave that line of code in or take it out; that’s entirely up to you.

Now it’s time to get serious. To begin with we set up a For Each loop to loop through the collection of files found in the folder C:\Scripts. The first thing we do inside that loop is check to see whether we’re working with a Word document (that is, a file with a .doc file extension):

If objFile.Extension = “doc” Then

Note. Yes, thanks for pointing that out: do not include the period when specifying a file extension in WMI. It’s doc, not .doc.

Let’s say that we do have a Word document. What then? Well, for starters, we use this line of code to open the document:

Set objDoc = objWord.Documents.Open(objFile.Name)

We can do this because the Name property gives us the complete path to the file. With the document open we can then use the ComputeStatistics method to determine the number of pages in the file:

intPages = intPages + objDoc.ComputeStatistics(wdStatisticPages)

As you can see, we’re using a variable named intPages to keep track of the total number of pages. What we’re doing here is determining the number of pages in our first document, something we do by calling ComputeStatistics and passing it the constant wdStatisticPages. We take the number of pages in document 1, add that to the current value of intPages, and then assign the sum of those two numbers to intPages.

Why? Well, the first time through the loop intPages will be equal to 0; that’s because we haven’t assigned it a value yet. Let’s assume that document 1 has 5 pages. That means our equation will look like this:

intPages = 0 + 5

That makes intPages equal to 5. Now, suppose we loop around and find a second Word document, this one containing 13 pages. That means our next step is to add those 13 pages to the current value of intPages (5). In other words:

intPages = 5 + 13

The variable intPages will now be equal to 18 which – magically enough – also equals the total number of pages we’ve found so far. Clever, huh?

Well, OK: kind of clever.

After we get the total number of pages for the first Word document we set the Saved property to True, which ensures that Word won’t ask us if we want to save changes to the document; we then close document 1. After that we loop around and repeat this process with the next file in the collection.

When we’re all done we use these two lines of code to echo back the total number of pages found and to terminate our instance of Microsoft Word:

Wscript.Echo “Total pages: ” & intPages

That should do the trick.

You know, that’s a good point: beriberi does sound pretty serious, doesn’t it? So then why did the Scripting Dad come in to work? To be honest, he had at least 200 reasons: $70 for the athletic participation fee; $40 for an ASB card; $40 for a yearbook; $50 for a parking pass.


Needless to say, we’ll see you tomorrow.

Comments (3)

  1. vijay says:

    Hi All,

    Could anyone please help me with a batch file or a macro to extract the no. of pages for each and every word file in a particular document

  2. Inebane says:

    In Office 2012 debugger stops at Wscript.Echo "Total pages: " & intPages. Any clues why? And more importantly how to fix it?

  3. thomas says:

    I’d like to be able to extract the number of words contained in all files contained in a folder.

    The example is that I have a folder containing all of the Excel workbooks, Word documents, and pdf files that I created during the completion of my bachelors degree.

    I wondered if there is a way to count up the total number of words in all of these folder. I can right click on the main folder and determine that the main folder contains 1.28 GB of data in the form of 2,795 files stored in 143 folders.

    I’d love to know how many words it took to build this volume of data and complete the degree.

    Thank you for your assistance.

Skip to main content