0
Answer

VB.NET AddIns Excel

fong chee chung

fong chee chung

12y
1.6k
1
Hi,
How to share the variable value between the two class?

I get the error message
System.NullReferenceException: Object reference not set to an instance of an object.



ThisAddIn.vb ----
Public Class ThisAddIn
    Dim xlApp As Excel.Application
    Dim strAddinName As String = "xyz.classCOM"
    Public shareMe As String = String.Empty
    Private Sub ThisAddIn_Startup() Handles Me.Startup
        Me.Application = CType(Globals.ThisAddIn.Application, Excel.Application)
        xlApp = Me.Application
        Try
            xlApp.AddIns.Add(Filename:=strAddinName)
            xlApp.AddIns(strAddinName).Installed = True
         Catch ex As Exception
            System.Windows.Forms.MessageBox.Show(ex.ToString)
        End Try
    End Sub
    Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown
        xlApp.AddIns(strAddinName).Installed = False
    End Sub
End Class



classCOM.vb ---
<System.Runtime.InteropServices.Guid("ad55814e-4cef-41f7-aab1-c123be605f8a")> _
<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDual)> _
<System.Runtime.InteropServices.ComVisible(True)> _
Public Class classCOM

    Public Function abc() As String
 
        Try
 
            return Globals.ThisAddIn.shareMe
 
        Catch ex As Exception
            Return "error"
        End Try
 
 
    End Function

    Public Function test() As String
        Return "apple"
    End Function
 
#Region " Setup "
 
    Public Sub New()
    End Sub
 
    <System.Runtime.InteropServices.ComRegisterFunctionAttribute()> _
    Public Shared Sub RegisterFunction(ByVal type As Type)
        Microsoft.Win32.Registry.ClassesRoot.CreateSubKey(GetSubKeyName(type, "Programmable"))
        Dim key As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(GetSubKeyName(type, "InprocServer32"), True)
        key.SetValue("", System.Environment.SystemDirectory & "\mscoree.dll", Microsoft.Win32.RegistryValueKind.[String])
    End Sub
 
    <System.Runtime.InteropServices.ComUnregisterFunctionAttribute()> _
    Public Shared Sub UnregisterFunction(ByVal type As Type)
        Microsoft.Win32.Registry.ClassesRoot.DeleteSubKey(GetSubKeyName(type, "Programmable"), False)
    End Sub
 
    Private Shared Function GetSubKeyName(ByVal type As TypeByVal subKeyName As StringAs String
        Dim s As New System.Text.StringBuilder()
        s.Append("CLSID\{")
        s.Append(type.GUID.ToString().ToUpper())
        s.Append("}\")
        s.Append(subKeyName)
        Return s.ToString()
    End Function
 
#End Region
 
 
End Class