3
votes

PowerPoint 2007 only exposes a single presentation close event (PresentationClose), which is raised before the closing of the presentation.

In several pieces of code I'm working on, I need to keep track of opened presentations, and therefore to react to one of them being closed.

Generally the event proposed by PowerPoint is enough. Except in the following case.

If the presentation has not been saved when it is closed, PowerPoint displays a dialog asking the user if he wants to save his presentation or not. If the user clicks yes or no, everything is fine since the presentation will eventually be closed. But he can also select to cancel closure...

In this case, the close event is raised, the presentation is still there but my application does not know it.

Can someone give me some kind of workaround? Maybe a event raised after the user clicks on cancel?

2

2 Answers

1
votes

You probably want PresentationBeforeClose or PresentationCloseFinal which was added in PowerPoint 2010.

You could also have this same problem occur if the user clicks 'Yes' to save at the prompt and then clicks 'Cancel' to exit the Save Presentation window. This still keeps the presentation alive within the application.

PowerPoint 2007 workaround I came up with (inspiration from here):


void Application_PresentationClose(PowerPoint.Presentation presentation)
{
    if (presentation.Saved == MsoTriState.msoFalse)
    {
        MessageBoxResult savePrompt = MessageBox.Show(string.Format("Do you want to save the changes you made to {0}?", presentation.Application.ActiveWindow.Caption), Globals.ThisAddIn.Application.Name, MessageBoxButton.YesNoCancel, MessageBoxImage.Warning, MessageBoxResult.Yes);
        if (savePrompt == MessageBoxResult.Yes)
            System.Windows.Forms.SendKeys.Send("Y"); // trigger default SaveAs prompt
        else if (savePrompt == MessageBoxResult.No)
            System.Windows.Forms.SendKeys.Send("N"); // discard presentation
        else
            System.Windows.Forms.SendKeys.Send("{ESC}"); // disables default SaveAs prompt
    }
}
0
votes

Something like this would do it, I think:

Private Sub PPTEvent_PresentationClose(ByVal Pres As Presentation)

  Dim x as Long
  with Application.Presentations
  If .Count > 0 Then
    For x = 1 to .Count
      If .Item(x) = Pres Then
         ' the presentation that triggered the event
         ' is still open; user must've canceled
      Else

      End If
    Next
  End if