Reputation: 51
We have designed Microsoft Addins for MS PowerPoint 2019 written in VB.net. There we have taken a counter (pptCounter) which increases whenever a presentation (either existing or new) opens. So we want to delete this open presentation after the counter becomes more than 1.But it didn't close the open file.
We have used this below given snippet :
Private Sub WorkWithDocument(ByVal Doc As Microsoft.Office.Interop.PowerPoint.Presentation) Handles Application.PresentationOpen
If (pptCounter > 1) Then
*Globals.Connect.Application.ActivePresentation.Close()*
End If
End Sub
Here is some more information about which environment I'm working in:
Operating System : Microsoft Windows 10 Pro
Code Editor : Visual Studio 2019
Technology : Vb.net(.Net Framework 4.8)
MS Office Version : 2019(32 bit) : Microsoft Windows 10 Pro
Upvotes: 0
Views: 226
Reputation: 1289
It is not the right thing to close the open document within the same handler: Application.PresentationOpen because it is possible that some processing is still going on the handler so add some additional handler which executes after opening the presentation.
E.g. Like you have shared following method
Private Sub WorkWithDocument(ByVal Doc As Microsoft.Office.Interop.PowerPoint.Presentation) Handles Application.PresentationOpen
Which must be having handler in the start-up AddHandler Application.NewPresentation, AddressOf WorkWithDocument
Now do one thing add additional handler AddHandler Application.NewPresentation, AddressOf AfterPresentationOpen
and call below method to close the presentation.
For reference:
Private Sub AfterPresentationOpen(ByVal Doc As Microsoft.Office.Interop.PowerPoint.Presentation) Handles Application.AfterPresentationOpen
If (pptCounter > 1) Then
Doc.Close()
End If
End Sub
It will be surely called after opening the presentation so there is a high possibility close the presentation.
Upvotes: 0
Reputation: 49455
The instance of the PowerPoint presentation is passed as a parameter to the method, so you need to use that object in the code instead of getting the active one:
Private Sub WorkWithDocument(ByVal Doc As Microsoft.Office.Interop.PowerPoint.Presentation) Handles Application.PresentationOpen
If (pptCounter > 1) Then
Doc.Close()
End If
End Sub
In some cases it is not correct to close presentations in the Open
event handler. So, I'd recommend letting the event handler finish its work and then close the just opened presentation right after the event handler is done. You can use a timer in VBA for scheduling a delayed action.
Upvotes: 0