【问题标题】:Microsoft Ribbon button to execute function from add-in用于从加载项执行功能的 Microsoft 功能区按钮
【发布时间】:2015-07-26 22:00:15
【问题描述】:

好的,所以我做了很多谷歌搜索,试图找到关于这个主题的信息,但我几乎空手而归。也许我没有为我想要完成的事情寻找正确的术语。

我的问题是我在 MS Excel 插件中编写了一个函数,我按照 Microsoft 的说明作为起点,但他们的教程在每次用户保存文档时都会执行代码。我的目标是在我设计的功能区上有一个按钮来执行此功能,而不是保存按钮。

这是我开始关注的 Microsoft 文章:https://msdn.microsoft.com/en-us/library/cc668205.aspx

我也在这里找到了这个问题,但没有足够的细节让我弄清楚如何为自己实施解决方案:How to connect a ribbon button to a function defined in an Excel add-in?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Excel;
namespace ExcelAddIn1
{
    public partial class ThisAddIn
    {
        void FormatTime(Microsoft.Office.Interop.Excel.Workbook WB, bool SaveAsUi, ref bool Cancel)
        {
            /////MY FUNCTION BODY HERE//////
        }
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }

        #endregion
    }
}

提前感谢您的帮助。

【问题讨论】:

    标签: c# excel vsto


    【解决方案1】:

    VSTO 提供了两种创建自定义 UI 的方法:

    1. 功能区设计师 - 请参阅 Walkthrough: Creating a Custom Tab by Using the Ribbon Designer

    2. 原始 XML 标记 - Walkthrough: Creating a Custom Tab by Using Ribbon XML

    在这两种情况下,您都可以使用Globals.ThisAddin 属性访问加载项属性和方法,该属性返回加载项类的实例(如上面列出的代码所示)。

    【讨论】:

      【解决方案2】:

      通常您可以使用 Globals.ThisAddIn.Application 访问应用程序级别和文档级别的 UI。 我希望this link 可以提供帮助。下面是向这样的工作表添加按钮的示例:

      Globals.Factory.GetVstoObject(
          Globals.ThisAddIn.Application.ActiveWorkbook.Worksheets[1])
              .Controls.AddControl(button, selection, buttonName);
      

      看起来像

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-27
        • 1970-01-01
        • 2012-09-10
        • 1970-01-01
        相关资源
        最近更新 更多