Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Visual Studio
 Calling Code in Application-Level A...
Microsoft Visual Studio Tools for the Microsoft Office system (version 3.0)
Calling Code in Application-Level Add-ins from Other Office Solutions

Applies to

The information in this topic applies only to the specified Visual Studio Tools for Office projects and versions of Microsoft Office.

Project type

  • Application-level projects

Microsoft Office version

  • 2007 Microsoft Office system

  • Microsoft Office 2003

For more information, see Features Available by Application and Project Type.

You can expose an object in your add-in to other Microsoft Office solutions. This is useful if your add-in provides a service that you want to enable other Office solutions to use. For example, if you have an add-in for Microsoft Office Excel that performs calculations on financial data from a Web service, other Office solutions can perform these calculations by calling into the Excel add-in at run time.

Visual Studio Tools for Office also provides a similar feature for document-level customizations. If you are developing a customization for the 2007 Microsoft Office system, you can call code in your customization from VBA code in the document. For more information, see Calling Code in Document-Level Customizations from VBA.

You can expose an object in an add-in to the following types of Office solutions:

  • Other Visual Studio Tools for Office add-ins.

  • Document-level customizations that are loaded in the same application process as your add-in.

  • COM add-ins (that is, add-ins that implement the IDTExtensibility2 interface directly).

  • Visual Basic for Applications (VBA) code in a document that is loaded in the same application process as your add-in.

To expose an object to other add-ins, override the RequestComAddInAutomationService method in the ThisAddIn class. Return the object that you want to expose to other Office solutions.

When your add-in is loaded, the Visual Studio Tools for Office runtime calls the RequestComAddInAutomationService method. The runtime assigns the returned object to the Object property of a COMAddIn object that represents your add-in. This COMAddIn object is available to other Office solutions.

Requirements of the Returned Object

Your implementation of RequestComAddInAutomationService must return an instance of a class that meets the following requirements:

If the object you return does not meet these requirements, the Visual Studio Tools for Office runtime will throw an InvalidCastException after it calls your implementation.

To see an example of a class that meets these requirements, see Walkthrough: Calling Code in an Application-Level Add-in from VBA.

Example

The following code example demonstrates how to override RequestComAddInAutomationService. This example assumes that you have defined a class named AddInUtilities that you want to expose to other Office solutions, and the AddInUtilities class meets the requirements specified above. To see this code in the context of a larger walkthrough, see Walkthrough: Calling Code in an Application-Level Add-in from VBA.

Visual Basic
Private utilities As AddInUtilities

Protected Overrides Function RequestComAddInAutomationService() As Object
    If utilities Is Nothing Then
        utilities = New AddInUtilities()
    End If
    Return utilities
End Function

C#
private AddInUtilities utilities;

protected override object RequestComAddInAutomationService()
{
    if (utilities == null)
        utilities = new AddInUtilities();

    return utilities;
}

In the code of a different Office solution (not the exposed add-in), perform the following steps:

  1. Get the COMAddIn object that represents the exposed add-in. You can access all of the available add-ins by using the COMAddIns property of the Application class.

  2. Access the Object property of the COMAddIn object.

The following code example demonstrates how to use VBA to call a method in an add-in. This VBA macro calls a method named ImportData that is defined in an add-in that is named ExcelImportData. To see this code in the context of a larger walkthrough, see Walkthrough: Calling Code in an Application-Level Add-in from VBA.

Sub CallVSTOMethod()
    Dim addIn As COMAddIn
    Dim automationbject As Object
    Set addIn = Application.COMAddIns("ExcelImportData")
    Set automationObject = addIn.Object
    automationObject.ImportData
End Sub
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content      
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker