drraman
drraman

Reputation: 11

Open powerpoint with vb.net, leave open and how to release objects when powerpoint is closed?

I have the following code to run a macro in a powerpoint file via VB.NET 2013 Express:

        'Start PowerPoint and open the presentation.
        Dim oPP = New Microsoft.Office.Interop.PowerPoint.Application()
        oPP.Visible = MsoTriState.msoTrue
        Dim oPresSet = oPP.Presentations
        Dim oPres = oPresSet.Open(PFile)
        oPP.Run("'Plan.ppt'!UpdatePlan")
        oPres = Nothing
        If Not oPresSet Is Nothing Then
            System.Runtime.InteropServices.Marshal.ReleaseComObject(oPresSet)
            oPresSet = Nothing
        End If
        oPP.Quit()
        If Not oPP Is Nothing Then
            System.Runtime.InteropServices.Marshal.ReleaseComObject(oPP)
            oPP = Nothing
        End If

I leave the powerpoint open to view and then close the file via powerpoint. When this code is run the code again, I get the following error with the following line of code:

        Dim oPP = New Microsoft.Office.Interop.PowerPoint.Application()

Error Message - "An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in Plan.exe

Additional information: Creating an instance of the COM component with CLSID {91493441-5A91-11CF-8700-00AA0060263B} from the IClassFactory failed due to the following error: 800706b5."

Is there any way to release the objects when powerpoint file is closed via powerpoint so that I can re-run the code again without error?

Is there another way to do this so that I don't get these errors?

Been working on this for ages and help would be much appreciated. Thanks.

Upvotes: 1

Views: 4892

Answers (1)

ruedi
ruedi

Reputation: 5555

To release the object run the following sub:

''' <summary>
''' This releases the object
''' </summary>
''' <remarks></remarks>
Sub ReleaseObj()
    _pptApp.Quit()
    _pptApp = Nothing
    _pptPre = Nothing
End Sub

To get more information check Hans Passant's answer and the links in this question.

Upvotes: 0

Related Questions