简体   繁体   中英

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.

All this code works without any problems in the Word add-in, but in the PowerPoint add-in I get an

"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. The method that is being called is 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() .

As mentioned I have identical code in a Word add-in which does not have this problem. 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.

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

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.

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. 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();
        }
   }

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