Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MS Word page count for all docs in a folder 3

Status
Not open for further replies.

steinfeld

IS-IT--Management
Feb 21, 2006
25
US
I am trying to write a macro that will give me a page count for all .doc in a folder. I have the code below. It works incrementing through the .docs but it returns the wrong page count. If there is 125 pages it will return 119.

Sub documentPageCount()

Dim oApp As Object
Dim oDoc As Object
Dim oTbl As Object
Dim sMyDir As String
Dim sDocName As String

'Start Word and open the document.
Set oApp = CreateObject("Word.Application")
oApp.Visible = True

' The path to obtain the files.
sMyDir = "C:\temp\"
sDocName = Dir(sMyDir & "*.DOC")
While sDocName <> ""

Set oDoc = oApp.Documents.Open(sMyDir & sDocName)

x = oDoc.Content.Information(wdActiveEndPageNumber)

oDoc.Close False

sDocName = Dir()
Wend

oApp.Quit

End Sub
 
Hmmm...Just guessing here:
Is it possible that section breaks in some of the document might throw off the page count?

Tom

Live once die twice; live twice die once.
 
Maybe you will find more help in a Vba Forum as Forum707 ?
 

What makes you say the document has 125 pages? Word doesn't have a proper concept of a page; it paginates documents according to printer settings as and when required. So if you happen to be using two different printer drivers you may get two different answers.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

Professional Office Developers Association
 
When I type activeDocument.Content.Information(wdActiveEndPageNumber)
into the imedeate window it gives me the proper total.
 

Do you get the same consistent wrong answer?

It may be one of those awkward timing issues which I hate because you don't have any real control.

When you run your code it may be that Word has not fully done the repagination (which it normally does in the background) and the VBA just returns what it thinks at the time. If this is the case you need to either (a) wait a bit which is really rather random, or (b) do something which forces full repagination - GoTo the last page perhaps

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

Professional Office Developers Association
 
Got it. I had to add
oDoc.Repaginate
before I get the count.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top