How to access an excel Add-In String parameter from excel workbook VSTO

163 Views Asked by At

I tried following the examples on MSDN. I have a String parameter that i need to access from VB.Net VSTO Excel WorkBook. I have opened up ComVisible in the Add-In and declared my interface, and declared a class to expose the interface. The function i want to expose is called CheckLicStatus() and it should return a string depending on the license status, either "Active" or "Expired". Help! I also feel that the way i am retrieving a string parameter from a passed object is wrong.

This is the Add-In Code exposing the function:

   'Open up ComVisible
    <ComVisible(True), InterfaceType(ComInterfaceType.InterfaceIsDual) _
    Public Interface IAddInLicense
        Function CheckLicStatus() As String
    End Interface

    'Decalre class to expose to out of process clients
    <ComVisible(True)> _
<ClassInterface(ClassInterfaceType.None)> _
    Public Class AddInLicense
        Implements IAddInLicense

        ' get license status 
        Public Function CheckLicStatus() As String Implements IAddInLicense.CheckLicStatus
           
            CheckLicStatus = ""
            
                    If LicIsActive Then
                        CheckLicStatus = "Active"
                        MsgBox("license is active")
                    Else
                        CheckLicStatus = "Expired"
                        MsgBox("License is Expired")
                    End If
            
        End Function
    End Class

    Private myLicense As AddInLicense
    Protected Overrides Function RequestComAddInAutomationService() As Object
        If myLicense Is Nothing Then
            myLicense = New AddInLicense()
        End If
        Return myLicense
    End Function

This is the VSTO excel code trying to access the value of the function:

    Public Sub CheckLicStatus()

        Dim addIn As Office.COMAddIn = Globals.ThisWorkbook.Application.COMAddIns.Item("EstimateAddIn")
        Dim myLicense As EstimateAddIn.IAddInLicense = TryCast( _
            addIn.Object, EstimateAddIn.IAddInLicense)
      
            Try
                licstatus = myLicense.CheckLicStatus()
                MsgBox(licstatus)
            Catch ex As Exception
                MsgBox("Could Not Get licstatus")
            End Try

    End Sub

I also have this code in a separate module:

Namespace EstimateAddIn
    Interface IAddInLicense

        Function CheckLicStatus() As String

    End Interface
End Namespace

0

There are 0 best solutions below