I'm trying to find a way of listing the number of pages within multiple word documents, all saved within the same folder. Is this possible using VBA?
I am using:
- Windows 7 and
- Microsoft Office 2007
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.
Sign up to join this communityI'm trying to find a way of listing the number of pages within multiple word documents, all saved within the same folder. Is this possible using VBA?
I am using:
I don't know how to do it in VBA (in Word itself) but you could create a small VBS-file and drop it in the directory and double click it:
Const wdStatisticPages = 2
Set FSO = CreateObject("Scripting.FileSystemObject")
currentPath = fso.GetAbsolutePathName(".")
set FLD = FSO.GetFolder(currentPath)
Set objWord = CreateObject("Word.Application")
objWord.Visible = False
For Each Fil In FLD.Files
If UCase(FSO.GetExtensionName(Fil.name)) = "DOCX" Then
Set objDoc = objWord.Documents.Open(currentPath & "\" & Fil.Name)
intPages = intPages + objDoc.ComputeStatistics(wdStatisticPages)
objDoc.Saved = True
objDoc.Close
End If
Next
objWord.Quit
Wscript.Echo "Total pages: " & intPages
Set oShell = Nothing
Set FLD = Nothing
Set FSO = Nothing
Assuming the files are all .docx
extension.
Inspiration (and explanation) came from here. For simplicity i didn't use objWMIService
but just went with Scripting.FileSystemObject
. You can also set objWord.Visible = False
to True
. You'll see the Word-application flickering on and off.
You can also show the pages in "Windows Explorer". Just right-click the columns above the files and choose more, you can select pages
to show a column with the pages per Word-document. So you can see the page-count before opening the document. (You'll need to be in the Details
-view to see the columns)
If you really want you could also translate this yourself to VBA (Word-macro).
See my previous answer to a similar question - this may help.
It IS possible to achieve, technically, but not always reliable - especially without being (computationally) expensive, which nobody ever wants - and my answer explains why.