Dmitry Kostochko

HowTo: Communicate with a COM add-in from a standalone application

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

15 Comments

  • Matt Comstock says:

    Hello,

    Thanks – I was able to utilize this code to create a VB.NET app to execute methods on my add-in. However, the instance of Excel 2010 (which my code starts up) is not closed when I call Quit on the Excel.Application instance. If I simplify the code to only load a workbook and get the add-in object without actually making any calls on it, the Excel instance disappears from Task Manager when my app terminates (surprisingly, NOT when the code calls Quit on the Excel.Application instance).

    We are using Add-In Express for .NET, version 6.4.3056. I can provide my sample application code if that would help. Thanks

    – Matt –

  • Eugene Astafiev says:

    Hello Matt,

    Most probably you don’t release underlying COM objects properly. Please read more about this in the When to release COM objects in Office add-ins developed in .NET article on our technical blog.

    Also you can send the sample code to the support e-mail address (see readme.txt). I will test it on our PCs.

  • ved says:

    Hello,

    I am trying to achieve the same functionality as discussed above, but in my case I am gettin addin.Object as null. Can any one tell me the reason?

  • Dmitry Kostochko (Add-in Express Team) says:

    Hi,

    I can assume that developers of that add-in did not set the Object property for some reason. Do you have access to the source code of that add-in? What technology is it based on? VSTO?

  • Dmitry Kostochko (Add-in Express Team) says:

    BTW, there may be a simpler reason – the add-in is not loaded, e.g. turned off or disabled by the user.

  • ved says:

    I found the root cause of the problem. I was getting addin object as null because of some windows related permission problem.

  • Mike says:

    Hello,

    This has been a huge help. I feel I am very close, but not quite. I am trying to invoke the Lync Meeting COMAddin in Outlook. My addin instance keeps coming up empty. It is definitely connected and at the ready.

    The program ID = UCAddin.LyncAddin.1

    Ultimately I am trying to create a Lync Online meeting invite programmatically, and this is my last hurdle.
    Thanks for any help you can provide.

    Mike

  • Dmitry Kostochko (Add-in Express Team) says:

    Hello Mike,

    I think the Lync Meeting Add-in does not set the Object property of the COMAddin interface and that is why you cannot access it. I would suggest that you try using the Lync SDK:
    Get started with Lync meetings
    How to: Start a meet-now meeting

  • Mike says:

    Hi Dmitry,

    I have been using the Lync SDK for other parts of this, but that does not allow me to schedule future meetings. I can create Meet Now meetings, but I can’t schedule one to take place 3 months from now.

    To do future meetings I have heard I can use UCMA SDK, but I am trying to avoid that.

    Another option was to use the UCAddinLib.dll, as that is what the addin uses, but there is no documentation on how to call the MakeOnlineMeeting function, besides what is in the object browser.

    A final option I was trying to do was find a way to iterate through the available buttons on the outlook ribbon and invoke the Lync Meeting button. I cant use MSO as that only works with the built in ribbon items.

    Any ideas?

    Mike

  • Dmitry Kostochko (Add-in Express Team) says:

    Hello Mike,

    Sorry, I am not an expert in the Lync SDK and therefore cannot give any advice.

    I have just checked my assumption about the Object property of the COMAddin interface and can confirm that the Lync Meeting Add-in does not set it.

    >> A final option I was trying to do was find a way to iterate through the available buttons on the outlook ribbon and invoke the Lync Meeting button.

    To invoke a ribbon button of a third-party add-in you need to know the idQ property (fully qualified id) of that button. Try to search through Lync documentation, if you find the idQ you will be able to click the button programmatically.

  • Mike says:

    Thanks for your help. I guess I am put off because I thought I would just be able to add a reference to UCAddinLib.dll and then call the MakeOnlineMeeting function on my appointment item. Seems logical to me, but I can’t figure out how to call it.

    Any reason why that wouldnt work?

    Mike

  • Dmitry Kostochko (Add-in Express Team) says:

    Hello Mike,

    I suppose, the reason is that UCAddinLib.dll was developed is such a way that it does not expect any calls from external code.

  • Mohamed BASSAMI says:

    Hello Mike,

    Have you found a solution to create online meeting using UCAddinLib.dll?

    Thank you in advance.
    Mohamed

  • Arun Goyal says:

    I am able to call this code using vb.net ,but with c#,i got an invalid cast exception.
    Can you please help me?

  • Andrei Smolin (Add-in Express Team) says:

    Hello Arun,

    I’ve downloaded “C# sample add-in for VS 2005” above and rebuilt it with no compile-time issues.

Post a comment

Have any questions? Ask us right now!