为VSTO应用程序创建跨项目功能区

时间:2017-08-26 23:51:12

标签: c# excel vsto

我正在寻找创建一个不同应用可以访问的功能区。基本上我有几个VSTO excel加载项,它们是单独构建的,并且有单独的安装文件。它们都有自己的功能区(虽然我确实在每个项目中为功能区提供相同的描述和名称)。有没有办法让它们(它们是应用程序)安装在excel用户界面中的单个功能区上?如果用户安装了多个应用程序,他们最终会得到两个名为完全相同的功能区部分。

我确实从2008年发现了此链接,但无法在Visual Studio 2017中使用2016 VSTO。

https://blogs.msdn.microsoft.com/vsto/2008/03/10/share-a-ribbon-customization-between-office-applications-norm-estabrook/

1 个答案:

答案 0 :(得分:1)

你可以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.

添加标记。

[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";
        }
    }
}

提供它。

private AddInUtilities utilities;

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

    return utilities;
}

从另一个加载项调用它。

object addInName = "ExcelImportData";  
Office.COMAddIn addIn = Globals.ThisAddIn.Application.COMAddIns.Item(ref addInName);  
ExcelImportData.IAddInUtilities utilities = (ExcelImportData.IAddInUtilities)addIn.Object;  
utilities.ImportData();