VDIwarrior Posted January 2, 2008 Share Posted January 2, 2008 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 More sharing options...
evilertoaster Posted January 3, 2008 Share Posted January 3, 2008 this should work- Func GetPages($filePath) $objWord = ObjCreate("Word.Application") $objDoc = $objWord.Documents.Open($filePath) return $objDoc.ComputeStatistics(2) EndFunc Link to comment Share on other sites More sharing options...
gruntydatsun Posted January 3, 2008 Share Posted January 3, 2008 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 More sharing options...
DW1 Posted January 3, 2008 Share Posted January 3, 2008 Still relies on Sleep ... but it's the best one so far. AutoIt3 Online Help Link to comment Share on other sites More sharing options...
VDIwarrior Posted January 3, 2008 Author Share Posted January 3, 2008 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 Link to comment Share on other sites More sharing options...
gruntydatsun Posted January 3, 2008 Share Posted January 3, 2008 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. Link to comment Share on other sites More sharing options...
VDIwarrior Posted January 3, 2008 Author Share Posted January 3, 2008 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 More sharing options...
gruntydatsun Posted January 3, 2008 Share Posted January 3, 2008 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 More sharing options...
Moderators big_daddy Posted January 3, 2008 Moderators Share Posted January 3, 2008 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) Link to comment Share on other sites More sharing options...
VDIwarrior Posted January 3, 2008 Author Share Posted January 3, 2008 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 More sharing options...
Moderators big_daddy Posted January 3, 2008 Moderators Share Posted January 3, 2008 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 More sharing options...
VDIwarrior Posted January 3, 2008 Author Share Posted January 3, 2008 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 More sharing options...
Moderators big_daddy Posted January 3, 2008 Moderators Share Posted January 3, 2008 This is perfect, just exactly what I wanted. Thank you, thank you !You're welcome! Glad I was able to help. Link to comment Share on other sites More sharing options...
hazed Posted January 4, 2008 Share Posted January 4, 2008 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 More sharing options...
Moderators big_daddy Posted January 4, 2008 Moderators Share Posted January 4, 2008 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. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now