简体   繁体   中英

Powershell 2 event handling

I'm trying to script Powerpoint with Powershell 2.0.

This site says there's a "PresentationOpen" event. However, Get-Member does not show this event. Also, when I try to do this:

register-objectevent $application PresentationOpen notification_event

it says: "Cannot register for event. An event with name 'PresentationOpen' does not exist."

Why is this event not accessible from PowerShell? Am I doing it wrong, and there is another way?

What I'm really trying to do is to wait until the presentation is fully loaded before I save it in another format. Not waiting causes PPT to freeze sometimes.

I'm grateful for any help!

PowerShell is pretty weak in COM support (it's a lot more like C# than it is like VB). In this case, you'll have to delegate the event. See the dispatches on this page: http://support.microsoft.com/kb/308825/EN-US/

There may be other (and better) ways to do this, but this should get you started:

$ppa = New-Object -ComObject PowerPoint.Application
$eventId = Register-ObjectEvent $ppa PresentationOpen -Action { "Hi" }
$ppa.Visible = 1
$ppa.Presentations.Open("Path\To\Presentation.ppt")

You would want to replace the script block after -Action on the second line with whatever code would do the processing/saving.

If there is any output from your event that you have registered, you can deal with it through the Receive-Job cmdlet, otherwise you can just simply add a loop similar to this right after the Open() method call to block further script execution until the slide deck has finished opening:

While ((Get-Job $eventId).State -neq "Completed") { Start-Sleep -m 250 }
Receive-Job $eventId

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM