In order to get a standalone application to communicate with a COM add-in, the application needs to have access to the running host application (Microsoft Excel in our case), get the COMAddins collection, find a needed instance of the add-in there and use reflection to call public methods or retrieve public properties.
You can use a function similar to the one below, the main thing in this function is the index variable, which is a ProgID of your COM add-in:
Private Function GetAddinObject() As Object
Dim app As Excel._Application = Nothing
Try
app = CType(Marshal.GetActiveObject("Excel.Application"), _
Excel._Application)
Catch
statusLabel1.Text = "Excel is not running."
End Try
If app IsNot Nothing Then
Try
Dim addins As Office.COMAddIns = app.COMAddIns
If addins IsNot Nothing Then
Try
' ProgID of our add-in
Dim index As Object = "ExcelAddin_VB.AddinModule"
Dim addin As Office.COMAddIn = Nothing
Try
addin = addins.Item(index)
Catch
statusLabel1.Text = "COM add-in is not registered."
End Try
If addin IsNot Nothing Then
Try
If (addin.Connect) Then
Return addin.Object
Else
statusLabel1.Text = "COM add-in is not enabled."
End If
Finally
Marshal.ReleaseComObject(addin)
End Try
End If
Finally
Marshal.ReleaseComObject(addins)
End Try
End If
Finally
Marshal.ReleaseComObject(app)
End Try
Return Nothing
End If
End Function
And then you can use reflection (the InvokeMember method) to call your add-in code:
Private Sub buttonMethod1_Click(_
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles buttonMethod1.Click
statusLabel1.Text = String.Empty
Dim addinInstance As Object = GetAddinObject()
If addinInstance IsNot Nothing Then
Try
Dim value As Object = addinInstance.GetType().InvokeMember( _
"Method1", BindingFlags.InvokeMethod, _
Nothing, addinInstance, New Object() {})
MessageBox.Show(value.ToString())
Finally
Marshal.ReleaseComObject(addinInstance)
End Try
End If
End Sub
BTW, in the same way two or more add-ins can communicate with each other…
You may also be interested in:
Quick way to develop Office COM add-ins
Develop COM add-in for Office 2010-2000 in .NET, step-by-step
Available downloads:
This sample add-in was developed using Add-in Express 2010 for Office and .net
C# sample add-in for VS 2005
VB.NET sample add-in for VS 2005





