【问题标题】:Create Cross-Project Ribbon for VSTO Apps为 VSTO 应用程序创建跨项目功能区
【发布时间】:2017-08-26 23:51:12
【问题描述】:

我希望创建一个可供不同应用访问的功能区。本质上,我有几个单独构建并具有单独安装文件的 VSTO excel 加载项。他们都有自己的功能区(尽管我在每个项目中都给功能区提供了相同的描述和名称)。有没有办法让它们(它们是应用程序)安装在 excel UI 的单个功能区上?如果用户安装了多个应用程序,他们最终会得到两个命名完全相同的功能区部分。

我确实找到了 2008 年的此链接,但无法在 Visual Studio 2017 中使用 2016 VSTO。

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

【问题讨论】:

    标签: c# excel vsto


    【解决方案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();  
    

    【讨论】:

      猜你喜欢
      • 2019-11-18
      • 1970-01-01
      • 1970-01-01
      • 2022-07-30
      • 1970-01-01
      • 1970-01-01
      • 2018-10-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多