Jump to content
Sign in to follow this  
VDIwarrior

How to reliably get number of pages in a Word document

Recommended Posts

VDIwarrior

Hi,

I have the following code snippet.

#include <word.au3>

Dim $oTemp1, $oTemp2, $path, $pages
$path = "E:\parenting.doc"
$oTemp1 = _WordCreate($path)
$oTemp2 = _WordDocGetCollection($oTemp1, 0)
Sleep(10000)
$pages = _WordDocPropertyGet($oTemp2, "pages")
MsgBox(0, "ok", $pages)

The problem is, if I dont include the sleep, then the value of $pages will be 1. If I insert the sleep, then I get the correct value of 61. One thing I have noticed is that _WordCreate seems to return immediately after the first page of the Word document is visible. Is there a way to make it wait till all pages have been read ?

thanks a lot

-V

Share this post


Link to post
Share on other sites
evilertoaster

this should work-

Func GetPages($filePath)
    $objWord = ObjCreate("Word.Application")
    $objDoc = $objWord.Documents.Open($filePath)
    return $objDoc.ComputeStatistics(2)
EndFunc

Share this post


Link to post
Share on other sites
gruntydatsun

Sleep(10000)

$pages = _WordDocPropertyGet($oTemp2, "pages")

I know it's a horribly blunt and kludgy answer but if you replace the above two lines with the below it works....

$temppages = _WordDocPropertyGet($oTemp2, "pages")

while 1
    Sleep(4000)
    $pages = _WordDocPropertyGet($oTemp2, "pages")
    if $pages = $temppages Then ExitLoop
    $temppages = $pages
WEnd

Share this post


Link to post
Share on other sites
VDIwarrior

this should work-

Func GetPages($filePath)
    $objWord = ObjCreate("Word.Application")
    $objDoc = $objWord.Documents.Open($filePath)
    return $objDoc.ComputeStatistics(2)
EndFunc
This works great.. thanks, really appreciate it ! Can you please also tell me how to

a. Attach $objDoc to a Window

b. Scroll to a given page number

Share this post


Link to post
Share on other sites
gruntydatsun

Still relies on Sleep :) ... but it's the best one so far.

Try the one evil toaster posted. I just tried it and it works instantly. Doesn't need to open an instance of word either.

Share this post


Link to post
Share on other sites
VDIwarrior

I know it's a horribly blunt and kludgy answer but if you replace the above two lines with the below it works....

$temppages = _WordDocPropertyGet($oTemp2, "pages")

while 1
    Sleep(4000)
    $pages = _WordDocPropertyGet($oTemp2, "pages")
    if $pages = $temppages Then ExitLoop
    $temppages = $pages
WEnd

thanks, but I really dont want to rely on sleep, because I might open a word document of any size, I dont know how long it'll take.

Share this post


Link to post
Share on other sites
gruntydatsun

thanks, but I really dont want to rely on sleep, because I might open a word document of any size, I dont know how long it'll take.

The sleep in this one is the delay in between comparing the current and last read page count. It shouldn't matter what size the file is. Until the last and current read are the same size it loops.

Share this post


Link to post
Share on other sites
big_daddy

Here is an optimized version of evilertoaster's code.

#include <Word.au3>

$sPath = "E:\parenting.doc"
$oWordApp = _WordCreate($sPath)
$oDoc = _WordDocGetCollection($oWordApp, 0)
$iPages = $oDoc.ComputeStatistics(2)
MsgBox(0, "ok", $iPages)

Share this post


Link to post
Share on other sites
VDIwarrior

Here is an optimized version of evilertoaster's code.

#include <Word.au3>

$sPath = "E:\parenting.doc"
$oWordApp = _WordCreate($sPath)
$oDoc = _WordDocGetCollection($oWordApp, 0)
$iPages = $oDoc.ComputeStatistics(2)
MsgBox(0, "ok", $iPages)
You guys are a great help. Thanks much. Can you please also tell me how to scroll to any given page number and set the cursor/range so that I can insert text using "range.InsertAfter" ?

Share this post


Link to post
Share on other sites
big_daddy

I hope this is what you are looking for...

#include <Word.au3>

$wdGoToPage = 1
$wdGoToFirst = 1

$sPath = "E:\parenting.doc"
$oWordApp = _WordCreate($sPath)
$oDoc = _WordDocGetCollection($oWordApp, 0)
$iPages = $oDoc.ComputeStatistics(2)

; Returns a range object that represents the start position of the page
$oRange = $oDoc.GoTo($wdGoToPage, $wdGoToFirst, Random(1, $iPages, 1))

Share this post


Link to post
Share on other sites
VDIwarrior

I hope this is what you are looking for...

#include <Word.au3>

$wdGoToPage = 1
$wdGoToFirst = 1

$sPath = "E:\parenting.doc"
$oWordApp = _WordCreate($sPath)
$oDoc = _WordDocGetCollection($oWordApp, 0)
$iPages = $oDoc.ComputeStatistics(2)

; Returns a range object that represents the start position of the page
$oRange = $oDoc.GoTo($wdGoToPage, $wdGoToFirst, Random(1, $iPages, 1))
This is perfect, just exactly what I wanted. Thank you, thank you !

Share this post


Link to post
Share on other sites
big_daddy

This is perfect, just exactly what I wanted. Thank you, thank you !

You're welcome! Glad I was able to help.

Share this post


Link to post
Share on other sites
hazed

You're welcome! Glad I was able to help.

I'm still too much of a nooby, where do you get these .ComputeStatistics(2) magic? I'm trying to deal with replacing text with new text that is underlined and the same font as the original text. I am completely lost.

Share this post


Link to post
Share on other sites
big_daddy

I'm still too much of a nooby, where do you get these .ComputeStatistics(2) magic? I'm trying to deal with replacing text with new text that is underlined and the same font as the original text. I am completely lost.

The Word Object Model Reference is a good starting place. Here is the ComputeStatistics Method.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.