简体   繁体   English

关闭PowerPoint演示文稿

[英]Closing a powerpoint presentation

I have created some Office add-ins where I track whether or not a document/presentation is closed and then perform some custom code before closing the document/presentation. 我创建了一些Office加载项,用于跟踪文档/演示文稿是否已关闭,然后在关闭文档/演示文稿之前执行一些自定义代码。

All this code works without any problems in the Word add-in, but in the PowerPoint add-in I get an 所有这些代码都可以在Word加载项中正常运行,但是在PowerPoint加载项中

"System.Runtime.InteropServices.COMException (0x80048240): Presentation (unknown member) : Invalid request. This operation cannot be performed in this event handler."-error when closing the PowerPoint application down. “ System.Runtime.InteropServices.COMException(0x80048240):表示(未知成员):无效的请求。无法在此事件处理程序中执行此操作。”-关闭PowerPoint应用程序时出错。 The method that is being called is presentation.Close(). 被调用的方法是presentation.Close()。

The method works fine if it is called as part of a Ribbon eventhandler, but if it is called by any of the Application-evens ( PresentationClose , PresentationBeforeClose or PresentationCloseFinal ) it fails to do the presentation.Close() . 如果该方法作为功能区事件处理程序的一部分被调用,则该方法工作正常,但是如果由任何Application-evens( PresentationClosePresentationBeforeClosePresentationCloseFinal )调用,则该方法将无法执行presentation.Close()

As mentioned I have identical code in a Word add-in which does not have this problem. 如前所述,我在Word加载项中具有相同的代码,但没有此问题。 I know that the eventhandling in the two products are different but I still can't figure out why it is a problem when the application is being shut down - or the presentation is closed - in PowerPoint. 我知道两种产品中的事件处理是不同的,但是我仍然无法弄清楚为什么在PowerPoint中关闭应用程序(或关闭演示文稿)时出现问题。

Hope somebody can come with some good input. 希望有人能提供一些好的意见。

I ran across something very similar; 我遇到了非常相似的事情; to get around some bug or other in PPT, I needed to close then reopen the current presentation, but was unable to close the presentation from within the event handler (makes sense, I suppose, since it's still handling an event that belongs to the presentation that triggered it). 为了解决PPT中的一些错误或其他问题,我需要关闭然后重新打开当前演示文稿,但是无法从事件处理程序中关闭该演示文稿(我想很有意义,因为它仍在处理属于该演示文稿的事件触发了它)。

I got around this by having the event handler load a form modelessly. 我通过让事件处理程序无模式加载表单来解决此问题。 That allows the code in the event handler to continue to the event handler's End Sub, so you're no longer in the event handler. 这样一来,事件处理程序中的代码就可以继续到事件处理程序的End Sub,因此您不再需要使用事件处理程序。

The initialization code in the form then closes the current document and unloads the form. 然后,表单中的初始化代码将关闭当前文档并卸载该表单。

There's no need to show the form, so the user never sees any of this. 无需显示表单,因此用户永远不会看到任何表单。

I tried many ways, but nothing works. 我尝试了很多方法,但是没有任何效果。 So its better to search for the powerpoint process and kill it through code. 因此,最好搜索powerpoint进程并通过代码将其杀死。 Hope this is what you were looking for. 希望这就是您想要的。

Process[] pros = Process.GetProcesses();
  for (int i = 0; i < pros.Count(); i++)
   {
   if (pros[i].ProcessName.ToLower().Contains("powerpnt"))
        {
          pros[i].Kill();
        }
   }

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

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