Jump to content

How to reliably get number of pages in a Word document


Recommended Posts

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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" ?
Link to comment
Share on other sites

  • Moderators

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))
Link to comment
Share on other sites

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 !
Link to comment
Share on other sites

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.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...