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

13 Comments

  • https://secure.gravatar.com/avatar/7707c8f0fe5b9741132090208256ac19?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G 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 -

  • https://secure.gravatar.com/avatar/bbd979ae5b772d0bc1f82065aa4ba146?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G 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.

  • https://secure.gravatar.com/avatar/655345c7faa88f66dbc04e2c979e3f91?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G 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?

  • https://secure.gravatar.com/avatar/ab4ec2858cfdf1e44dadf8c50fae314d?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G 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?

  • https://secure.gravatar.com/avatar/ab4ec2858cfdf1e44dadf8c50fae314d?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G 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.

  • https://secure.gravatar.com/avatar/655345c7faa88f66dbc04e2c979e3f91?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G ved says:

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

  • https://secure.gravatar.com/avatar/111ce7f5858f50343a254b97710029fe?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G 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

  • https://secure.gravatar.com/avatar/ab4ec2858cfdf1e44dadf8c50fae314d?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G 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

  • https://secure.gravatar.com/avatar/111ce7f5858f50343a254b97710029fe?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G 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

  • https://secure.gravatar.com/avatar/ab4ec2858cfdf1e44dadf8c50fae314d?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G 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.

  • https://secure.gravatar.com/avatar/111ce7f5858f50343a254b97710029fe?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G 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

  • https://secure.gravatar.com/avatar/ab4ec2858cfdf1e44dadf8c50fae314d?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G 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.

  • https://secure.gravatar.com/avatar/9bae3e77832ec51998043d9d211b2697?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Mohamed BASSAMI says:

    Hello Mike,

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

    Thank you in advance.
    Mohamed

Post a comment

Have any questions? Ask us right now!