VSTO for Office 2010 Globals.Thisaddin is always null

177 views Asked by At

I have a Powerpoint addin, which when I need to reference Globals.ThisAddin.Application (the current instance or active instance of powerpoint calling to the addin) It returns a null reference. I do need `Globals.ThisAddIn.Application' or another method to get the current active instance of Powerpoint because it is used to generate something in the active presentation's active slide.

Watches

I tried using watches to track the error, but it says the following.

Current Watches

Code

VBA

Private Sub tst()
Dim tmp As object
Set tmp = New object
tmp.dosomething ' error triggered is here
Set tmp = Nothing
End Sub

Simplified VB.NET

ThisAddin

Imports Microsoft.Office.Interop.PowerPoint
Imports System.Diagnostics
Public Class ThisAddIn
    Private Sub ThisAddIn_Startup() Handles Me.Startup
    End Sub

    Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown

    End Sub

End Class

Custom Namespace and Class

Imports System.Data
Imports System.Diagnostics
Imports System.Runtime.InteropServices
Imports Ppt = Microsoft.Office.Interop.PowerPoint

Namespace CustomStuff

    <ComVisible(True)> _
    <InterfaceType(ComInterfaceType.InterfaceIsIDispatch)> _
    Public Interface IObject
        Sub DoSomething()
    End Interface


    <ComVisible(True)> _
    <ClassInterface(ClassInterfaceType.None)> _
    Public Class Obj
        Implements IObject
        <ComVisible(True)>
        Public Sub DoSomething() Implements IObject.DoSomething
            Dim cpres As Ppt.Presentation  
            cpres = Globals.ThisAddIn.Application.ActivePresentation ' <- error is here
            With cpres.Slides(cpres.Windows(1).Selection.SlideRange.SlideIndex)
            ...
            End With
        End Sub
        Private Sub New()

        End Sub
        <ComVisible(True)>
        Protected Overrides Sub Finalize()
            MyBase.Finalize()
        End Sub
    End Class

End Namespace

Software And IDE

Currently Available Software

1

There are 1 answers

1
Eugene Astafiev On

The ThisAddIn class comes from a VSTO Add-in project. You can access this object by using the Globals.ThisAddIn property. But I don't see the code of add-in in your sample. It seems you are trying to use the Application property outside of boundaries of the add-in.

See Global access to objects in Office projects for more information.