Jump to content

several questions (autoit3 + word (#pages), powerpoint (#slides)


krystian
 Share

Recommended Posts

Hello,

Instead of making a couple topics for each question individually, and since they have some grouping in subject matter, I have grouped them in this post. I hope that's ok, though I can seperate posts if necessary.

1. How do I detect how many slides are in a powerpoint show? I know in powerpoint's slide properties, I can get slide "Statistics" but I'm wondering if there's a simple way to send a query to a slide file or an open slide and get a response back (even if I have to parse it) without having to do some gui controls (click, select, use menus, etc).

2. How do I detect the number of pages in a word doc? The following link shows how to do so using windows scripting however I'm wondering how best to do this by querying the document perhaps through autoit.

http://www.microsoft.com/technet/scriptcen...06/hey0907.mspx

Again, like above in #1, I know properties has this option but perhaps there's a smart way to do this.

3. How do I detect what page # I'm on in an open word document? I am currently planning on perhaps reading the pixel at the bottom of the screen to the left which indicates page number, but perhaps there is a better way. (as that might take some gymnastics and figuring out how to deal with multinumber pages [ex: page 1 vs. page 11, vs. page 10, etc... yuck)

4. How to know how much time remains until a screensaver loads? This might be tricky. Searching through the forums hasn't provided me substantial results just yet. Why: Would like to know how to determine if screen saver is on or not. I suppose I can ward off a screensaver by moving a mouse around.

Any ideas or best practices are welcome!,

Krystian.

Edited by krystian
Link to comment
Share on other sites

2. How do I detect the number of pages in a word doc? The following link shows how to do so using windows scripting however I'm wondering how best to do this by querying the document perhaps through autoit.

http://www.microsoft.com/technet/scriptcen...06/hey0907.mspx

I coded this off your link and tried it, which worked:

Global Const $wdStatisticPages = 2
$DocFile = @ScriptDir & "\Test.doc"

$iAnswer = _WordDocGetPageCount($DocFile)
If Not @error Then
    MsgBox(64, "Results", "There are " & $iAnswer & " pages in " & $DocFile)
Else
    MsgBox(16, "Error", "Error occured getting page count from file.  @error = " & @error)
EndIf

Func _WordDocGetPageCount($sFile)
    If Not FileExists($sFile) Then Return SetError(1, 0, 0)

    $oWord = ObjCreate("Word.Application")
    If Not IsObj($oWord) Then Return SetError(2, 0, 0)

    $oDoc = $oWord.Documents.Open ($sFile)
    If Not IsObj($oDoc) Then Return SetError(3, 0, 0)

    $iPages = $oDoc.ComputeStatistics ($wdStatisticPages)

    $oDoc.Saved = True
    $oWord.Quit

    Return $iPages
EndFunc   ;==>_WordDocGetPageCount

...which made me feel smart.

Then I found __WordDocPropertyGet(), which is now included with AutoIt in the Word.au3 UDF, and now I'm back to stupid again... :)

Similar cade can be used against the "PowerPoint.Application" object, though to get what you want, and I don't see an included UDF for that.

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Similar cade can be used against the "PowerPoint.Application" object, though to get what you want, and I don't see an included UDF for that.

Hmm... when I tried it, I got an error:

$oMyError = ObjEvent("AutoIt.Error", "MyErrFunc") ; Install a custom error handler
Global $ErrHex = 0, $ErrDesc = ""

$PPTFile = @ScriptDir & "\Test.ppt"

$iAnswer = _PowerPointSlideGetCount($PPTFile)
If Not @error Then
    MsgBox(64, "Results", "There are " & $iAnswer & " slides in " & $PPTFile)
Else
    MsgBox(16, "Error", "Error occured getting slide count from presentation." & @CRLF & _
            "@error = " & @error & @CRLF & _
            "@extended = " & @extended)
EndIf

Func _PowerPointSlideGetCount($sFile)
    If Not FileExists($sFile) Then Return SetError(1, 0, 0)

    $oPPT = ObjCreate("PowerPoint.Application")
    If Not IsObj($oPPT) Then Return SetError(2, $ErrHex, 0)

    $colPresentations = $oPPT.Presentations
    If Not IsObj($colPresentations) Then Return SetError(3, $ErrHex, 0)

    $oPresentation = $colPresentations.Open ($sFile)
    If Not IsObj($oPresentation) Then Return SetError(4, $ErrHex, 0)
    
    $iSlides = $oPresentation.Slides.Count
    If @error Then Return SetError(5, $ErrHex, 0)

    $oPresentation.Saved = True
    $oPPT.Quit

    Return $iSlides
EndFunc   ;==>_PowerPointSlideGetCount

Func MyErrFunc()
    $ErrHex = Hex($oMyError.number, 8)
    $ErrDesc = $oMyError.windescription
    Return SetError(-1)
EndFunc   ;==>MyErrFunc

The Object reference "$colPresentations.Open ($sFile)" returns a COM Error 0x80020009. No description is returned, so I'm not sure how to follow up on it...

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Hey guys, your replies are great. I didn't even know we had word and ppt support like this!

I'm still baffled on quickly getting the page number I'm currently viewing in a word document. Like when I'm viewing page 2, for it to tell me I'm on page 2! hmph.

Where would I find this? DLL stuff?

I found these great postings, but I'm trying for the autoit approach:

google search for: +word +document +current +page +number

determine page number in Word document - microsoft.public.excel.programming | Google Groups

http://groups.google.com/group/microsoft.public.excel.programming/browse_thread/thread/f9b2622167216d7f/f2cc3ee002b994e2?lnk=st&q=%2Bword+%2Bdocument+%2Bcurrent+%2Bpage+%2Bnumber&rnum=7#f2cc3ee002b994e2

Insert current page number "Page X of N" on a word document - MSDN Forums

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=274470&SiteID=1

I could look into how autoit works with vba:

finding out current page number - microsoft.public.word.vba.general | Google Groups:

http://groups.google.com/group/microsoft.public.word.vba.general/browse_thread/thread/9864bb16ee79f8a7/ff21f63fb79f01f7?lnk=st&q=%2Bword+%2Bdocument+%2Bcurrent+%2Bpage+%2Bnumber&rnum=16#ff21f63fb79f01f7
Link to comment
Share on other sites

There is a property in the Word Document Object Model that would look like this:

$oWord.Activedocument.Selection.Information("wdActiveEndPageNumber")

But I don't have time to figure out what the selection has to be for that to work right now.

Hope that helps.

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

  • Moderators

Try this...

#include <Word.au3>

Const $wdActiveEndPageNumber = 3

$oWordApp = _WordCreate(@ScriptDir & "\Test.doc", 1)
$oDoc = _WordDocGetCollection($oWordApp, 0)
$iPage = $oWordApp.Selection.Information($wdActiveEndPageNumber)
$iPages = _WordDocPropertyGet($oDoc, "pages")
ConsoleWrite("Page " & $iPage & " of " & $iPages & @CR)
Link to comment
Share on other sites

Try this...

#include <Word.au3>

Const $wdActiveEndPageNumber = 3

$oWordApp = _WordCreate(@ScriptDir & "\Test.doc", 1)
$oDoc = _WordDocGetCollection($oWordApp, 0)
$iPage = $oWordApp.Selection.Information($wdActiveEndPageNumber)
$iPages = _WordDocPropertyGet($oDoc, "pages")
ConsoleWrite("Page " & $iPage & " of " & $iPages & @CR)
Cool. To do that with Word already open to see what the current page was would start with _WordAttach(), but I'm not sure how the selection would be made so $iPage represented the current page (cursor location?) vice the first page in the doc. (I live on OpenOffice.org, so I don't have Word to kick around here...)

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

I think, perhaps, I should be going about this differently.

So let me give background what I've been doing with this thread's information:

So I love to print my powerpoint slides with notes through the "File -> Send To -> Microsoft Word - OK > " options. In doing so,

Microsoft word opens and a lot of processing happens where slides are injected into MS word into a table and with notes next to it in a column. I

absolutely love this functionality.

However, and here is my question, how can I detect when this process of preparing the word document is complete? I know there are some commercial

products which offer to do handout generation for you, but I just want to detect when the process of preparing a word doc is complete so I can script

it with Autoit3.

I would like to know if autoit3 vb or something can be used to detect when this process completes. I used process explorer and poked around for a couple of hours to try to see precisely what it was that active when "Send To" is operating, but maybe someone would like to work with me if this is the proper way to do it.

Currently, I know I can detect when this is done by:

1. (best idea I know of right now) Monitoring somehow the word doc generation process which got started in powerpoint and seeing when it completes and monitoring an exit code (in case it terminates due to an error not due to natural causes). When it successfuly completes, then move on. (which I detail in my question above)

2. (ok idea) When the word document creation process is complete, the word document goes to page 1. Perhaps detecting when page 1 shows, would tell me.

However, what if my powerpoint presentation is less than 3 slides? Then I'll only have a page.... hmm. Problematic...

3. (more crazy of an idea?) Whilst the process is going on, a mouse cursor intermittently shows up as an hour glass and then rescinds into a cursor.

4. (most crazy of an idea?) Perhaps printing slides in powerpoint to postscript, timeing how long that takes, then doing the "Send To - > word"

behaviour using the previous printing in powerpoint time and multiplying it by some number to scale.

Edited by krystian
Link to comment
Share on other sites

I was hoping to see something like IE.au3's _IELoadWait().

Looking in the help file at the _Word* UDF functions, in particular _WordPropertyGet ($oWord, "windowstate"), I thought you might get "busy" or "done", but it only tells you "minimized", "normal", or "maximized".

What you're looking for is a property called "readystate", but I can only find it for IE browser objects, not Word app or Word doc objects.

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Hey, just so you guys know and because I also went down the path of extracting to get the number of slides, I just found this to maybe be useful:

"Dsofile.dll"

The Dsofile.dll files lets you edit Office document properties when you do not have Office installed

http://support.microsoft.com/?kbid=224351

The Scripting Guys' First Blog : Cleaing Out the Attic: Dsofile.exe:

http://blogs.msdn.com/gstemp/archive/2004/02/25/79867.aspx

I found out how to do this with autoit3 here:

Querying a file's METADATA - AutoIt Forums:

http://www.autoitscript.com/forum/index.ph...&hl=dsofile

(of course for the future non-vb programmer, here's how to convert vb code to autoit:

Help rewriting VBA COM to AU3 - AutoIt Forums:

http://www.autoitscript.com/forum/index.php?showtopic=44914

Link to comment
Share on other sites

Thanks PsaltyDS, still trying to figure that one [how to detect if completed processing a word document] too.

<s>The only problem with getting the #of pages from the word document generated is the file hasn't been saved yet, so I don't have a filename to work with while it is generating the word doc from the ppt.<s/>

I striked out my comment, since WordAttach includes the option to search for an open word document by window handler.

Edited by krystian
Link to comment
Share on other sites

Also, even when I registered the DLL and ran the demo code for a simple metadata lookup where code is at:

http://www.autoitscript.com/forum/index.ph...&hl=dsofile

http://www.autoitscript.com/forum/index.ph...mp;#entry334794

it failed. I get the folowing message:

"Error: Variable must be of type "Object"

Which according to these posts (seperate topics... you'll see) [1][2], means to me that something may not be getting set properly. Any ideas? Would someone like to confirm this with autoit 3.2.8.1 ?

There's nothing in the bug forum to suggest this is a bug or anything (error after 2 lines of code??). So I'm assuming I'm forgetting something.

[1] #141172

[2] #309909

Edited by krystian
Link to comment
Share on other sites

Actually, metadata lookup for the number of pages wasn't the best idea.

Summary: The metadata value for the number of pages sometimes didn't update when I made word docs to test with.

For instance, I made a word doc, scribbled some text and copied and pasted to make it fit 3 pages with some page breaks. Saved. The metadata showed only 1 page.

I'm using Office XP, which might be why this is a problem (since it is older and perhaps stuff has updated in the newer versions making this an non-issue). Nevertheless, this might be a good example why not to use the dso dll to read metadata... sometimes it's downright inaccurate! (weird stuff might happen, like above) (especially if you can write to the metadata too!)

Edited by krystian
Link to comment
Share on other sites

  • Moderators

Any new leads on this? The newsgroups I have sought advice from haven't responded with any new insights either.

Sorry it's taken so long to get back to you.

I was hoping PowerPoint exposed the "Send To -> Microsoft Word" through COM allowing us to call a method and wait for the return. However I was not able to find such. I also looked for a way to tell if PowerPoint or Word were not in an idle state, no luck there either.

The following is the best solution I could find. Start the script, execute "Send To -> Microsoft Word" from PowerPoint, watch the console output.

#include <Word.au3>

$oWordApp = 0
$iScrolled = 0

_WordErrorHandlerRegister()
_WordErrorNotify(False)
While Not IsObj($oWordApp)
    Sleep(100)
    $oWordApp = _WordAttach("Document1 - Microsoft Word", "title")
WEnd
_WordErrorNotify(True)
$oDoc = _WordDocGetCollection($oWordApp, 0)
ConsoleWrite("Attached to document..." & @CR)
While $iScrolled = 0
    Sleep(100)
    $iScrolled = $oDoc.Activewindow.VerticalPercentScrolled
WEnd
ConsoleWrite("Processing started..." & @CR)
While $iScrolled <> 0
    Sleep(100)
    $iScrolled = $oDoc.Activewindow.VerticalPercentScrolled
WEnd
ConsoleWrite("Processing finished!" & @CR)
Link to comment
Share on other sites

  • 2 months later...

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...