jc88
jc88

Reputation: 21

PowerPoint process won't close when user closes the main window

If I create a PowerPoint application via automation, the process remains in Task Manager when the user closes the main window. I can assume this is because my application is holding a reference to the PowerPoint app so it can't be garbage collected. But how can I know that the user closed the PowerPoint application so I can release all references to it? Seems like a chicken or the egg issue here. Below is the function I call on my form load. If PowerPoint is already opened, it uses the current instance. If not, it creates a new instance.

Private Function TryAttachToApplication() As PowerPoint.Application
        Dim app As PowerPoint.Application

        Try
            app = CType(Marshal.GetActiveObject("PowerPoint.Application"), PowerPoint.Application)
        Catch ex As COMException
            app = New PowerPoint.Application
        End Try

        app.Visible = True
        Return app
End Function

Upvotes: 2

Views: 2030

Answers (1)

Steve Rindsberg
Steve Rindsberg

Reputation: 3528

If PowerPoint is already opened, it uses the current instance. If not, it creates a new instance.

I don't think so. PowerPoint only allows one instance of itself, though at times it can mistakenly leave zombie instances behind when automated. It doesn't behave like Word or Excel, where you can create add'l instances of your own as needed.

If there's already an instance running, you'll get a reference to it. If there's no running instance, you'll create a new instance and get a reference to that.

Releasing references to the PPT instance shouldn't cause any problems. If the user had it open to start with, it should stay open.

The problem is more likely: "Should I call the PPT application's .Quit method or not?"

One approach to that is to check app.Presentations.Count I don't recall whether PPT will create a new blank presentation automatically when invoked via Automation; as I recall, it doesn't, but you'll want to doublecheck that. IAC, if .Count > 0 (or perhaps 1), you know you've gotten a reference to a pre-existing instance of PPT ... ie, somebody's already using it ... so you don't want to quit on them.

Upvotes: 1

Related Questions