How to add a global variable to your AddIn project (VB)

It's simple for your Visual Basic add-in projects/project template to provide a global variable, like 'ThisApplication'.  This kind of variable gives the end user convenient, VBA-like access to your host application's object model.

 

To define a project-wide global variable, you can declare a Friend  or Public variable inside a Module:

 

Module globalsMod

    Friend ThisApplication As ApplicationObject

End Module

 

 

We've declared a global variable ThisApplication as an instance of the host application's object model, the type 'ApplicationObject'.

When declared as a Friend  or Public variable, ThisApplication can be used by any class or module in the project. 

Notice below, that the global variable  ThisApplication is assigned in MacroAddIn_Startup() in the entrypoint class

Notice the assignment of ThisApplication in  MacroAddIn class and its use in both MacroAddIn.Macro1() and Module1.TestModule():

 

Module Module1

   

    Sub TestModule()

        MsgBox(ThisApplication.Caption)

    End Sub

 

End Module

==

<System.AddIn.AddIn("AppAddIn", Version:="1.0", Publisher:="", Description:="")> _

Partial Class MacroAddIn

 

    Public Sub Macro1()

        MsgBox(ThisApplication.Caption)

    End Sub

 

#Region "Startup Code"

 

    Private Sub MacroAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup

 

        ThisApplication = CType(Me.GetOM(), ApplicationObject)

 

    End Sub

 

    Private Sub MacroAddIn_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown

 

    End Sub

#End Region

 

End Class

 

The variable declaration and hookup code should be concealed from the end user by hiding them in a hidden file (see Adding a Hidden File to a Project Template)

Since our global ThisApplication is a reference to the host application's object model, it should never be assigned by the end user.  A better and safer approach would be to expose the global variable as a ReadOnly Property like this:

Module globalModule

    Private _thisApplication As ApplicationObject

    Public ReadOnly Property ThisApplication() As ApplicationObject

        Get

            Return _thisApplication

        End Get

    End Property

 

    Sub _AssignApplicationObject(ByVal thisApplication As ApplicationObject)

        _thisApplication = thisApplication

    End Sub

End Module

 

<System.AddIn.AddIn("AppAddIn", Version:="1.0", Publisher:="", Description:="")> _

Partial Class MacroAddIn

 

    Public Sub Macro1()

        MsgBox(ThisApplication.Caption)

    End Sub

 

#Region "Startup Code"

 

    Private Sub MacroAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup

        _AssignApplicationObject(CType(Me.GetOM(), ApplicationObject))

    End Sub

 

    Private Sub MacroAddIn_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown

 

    End Sub

#End Region

 

 

End Class

 


Posted Apr 24 2009, 03:06 PM by Gary
Copyright Summit Software Company, 2008. All rights reserved.