Powershell 2 事件处理

发布于 2024-09-28 07:02:30 字数 465 浏览 1 评论 0原文

我正在尝试使用 Powershell 2.0 编写 Powerpoint 脚本。

网站表示有一个“PresentationOpen”事件。但是,Get-Member 不显示此事件。另外,当我尝试执行此操作时:

register-objectevent $application PresentationOpen notification_event

它说:“无法注册事件。名称为“PresentationOpen”的事件不存在。”

为什么无法从 PowerShell 访问此事件?难道是我做错了,还有别的办法吗?

我真正想做的是等到演示文稿完全加载后再将其保存为其他格式。不等待有时会导致 PPT 冻结。

我很感激任何帮助!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你怎么敢 2024-10-05 07:02:30

PowerShell 在 COM 支持方面相当薄弱(它更像 C#,而不是 VB)。在这种情况下,您必须委托该事件。请参阅此页面上的调度:http://support.microsoft.com/kb/308825 /EN-US/

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/

痴意少年 2024-10-05 07:02:30

可能还有其他(和更好的)方法来执行此操作,但这应该可以帮助您开始:

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

您可能希望将第二行 -Action 之后的脚本块替换为执行处理/保存的任何代码。

如果您注册的事件有任何输出,您可以通过 Receive-Job cmdlet 处理它,否则您只需在 Open() 方法调用之后添加一个与此类似的循环即可阻止进一步的脚本执行,直到幻灯片已完成打开:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文