You can make an interface to your application-level Add-In available to other Add-Ins.
You can expose an object in an VSTO Add-in to the following types of solutions:
- Visual Basic for Applications (VBA) code in a document that is loaded in the same application process as your VSTO Add-in.
- Document-level customizations that are loaded in the same application process as your VSTO Add-in.
- Other VSTO Add-ins created by using the Office project templates in Visual Studio.
- COM VSTO Add-ins (that is, VSTO Add-ins that implement the IDTExtensibility2 interface directly).
- Any solution that is running in a different process than your VSTO Add-in (these types of solutions are also named out-of-process clients). These include applications that automate an Office application, such as a Windows Forms or console application, and VSTO Add-ins that are loaded in a different process.
Add markup.
[ComVisible(true)]
public interface IAddInUtilities
{
void ImportData();
}
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public class AddInUtilities : IAddInUtilities
{
// This method tries to write a string to cell A1 in the active worksheet.
public void ImportData()
{
Excel.Worksheet activeWorksheet = Globals.ThisAddIn.Application.ActiveSheet as Excel.Worksheet;
if (activeWorksheet != null)
{
Excel.Range range1 = activeWorksheet.get_Range("A1", System.Type.Missing);
range1.Value2 = "This is my data";
}
}
}
Provide it.
private AddInUtilities utilities;
protected override object RequestComAddInAutomationService()
{
if (utilities == null)
utilities = new AddInUtilities();
return utilities;
}
Call it from another add-in.
object addInName = "ExcelImportData";
Office.COMAddIn addIn = Globals.ThisAddIn.Application.COMAddIns.Item(ref addInName);
ExcelImportData.IAddInUtilities utilities = (ExcelImportData.IAddInUtilities)addIn.Object;
utilities.ImportData();