2
votes

I am creating a Powerpoint Add-in. I would like to restrict user from either:

  • Create New Presentation
  • Open an existing Presentation

I have used this tutorial/overview to trap the NewPresentation and PresentationOpen events. I would like to close any presentation initialized through these events before the user can interact with it. When I try to close it using .Close method, I receive an error (screenshot below).

In my class module, I have the following to trap the NewPresentation event. This works fine, I receive the message box and Pres is a valid Presentation object that can be passed to the CloseNewPres routine.

Private Sub PPTEvent_NewPresentation(ByVal Pres As Presentation)
MsgBox "You cannot use this Charting tool with multiple presentations.", vbInformation
CloseNewPres Pres
End Sub

In a standard module, I have the CloseNewPres routine, which I expect to close the "New" presentation:

Sub CloseNewPres(Pres As Presentation)
    Application.Presentations(Pres.Name).Close
    'Pres.Close '<~~ This also fails.'
End Sub

I receive the following error.

Screenshot of error message

Any thoughts on why this is happening? Or what I can do to close these presentations?

3

3 Answers

3
votes

In my opinion you need to use another event which is quite similar to one you used:

Private Sub PPApp_AfterNewPresentation(ByVal Pres As Presentation)

If I set Pres.Close within proposed one it really closes new created presentation.

1
votes

The best explanation I have (and this is my own interpretation) is that it's barking because you're trying to remove an object (the presentation) while the event handler is dealing with it. Kind of asking the event handler to pull the rug out from under its own feet.

By the way, it won't work to call another routine from within the event handler and have IT do the deed because the event handler's still active.

One way around this:

Have the event handler load a form modelessly. When you do that, the remaining code in the event handler runs to completion. The form's initialization code can close the presentation if your conditions are met.

The form needn't ever become visible for this to work.

Yep. Seems bizarre. But it works.

1
votes

In C#.Net you can do by this way,

using pp = Microsoft.Office.Interop.PowerPoint;

pp.Application app = Globals.ThisAddIn.Application;
app.AfterNewPresentation += Event_PresNew;

public void Event_PresNew(pp.Presentation pres)
{
//.....your code here....
}

Also you can see the list of all the events you can use in powerpoint here below, https://docs.microsoft.com/en-us/office/vba/api/powerpoint.application.newpresentation(even)