简体   繁体   English

Powershell 2事件处理

[英]Powershell 2 event handling

I'm trying to script Powerpoint with Powershell 2.0. 我正在尝试使用Powershell 2.0编写Powerpoint脚本。

This site says there's a "PresentationOpen" event. 网站说有一个“ PresentationOpen”事件。 However, Get-Member does not show this event. 但是,Get-Member不显示此事件。 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." 它说:“无法注册事件。名称为'PresentationOpen'的事件不存在。”

Why is this event not accessible from PowerShell? 为什么无法从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. 不等待有时会导致PPT冻结。

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). PowerShell对COM的支持非常薄弱(它比CB更像C#,而不是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/ 请参阅此页面上的调度: 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. 您可能想在第二行的-Action之后用任何可以进行处理/保存的代码替换脚本块。

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: 如果您注册的事件有任何输出,则可以通过Receive-Job cmdlet处理它,否则,您可以在Open()方法调用之后直接添加与此类似的循环以阻止进一步的脚本执行,直到滑板已完成打开:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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