Jump to content

Recommended Posts

Posted

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

Posted

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
Posted

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

Posted

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.

Posted

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.

  • Moderators
Posted

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)
Posted

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" ?
  • Moderators
Posted

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))
Posted

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 !
Posted

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.

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
×
×
  • Create New...