簡體   English   中英

Microsoft功能區按鈕,可從加載項執行功能

[英]Microsoft Ribbon button to execute function from add-in

好的,因此,我已經進行了大量的搜索工作以查找有關此主題的信息,並且我幾乎是空手而歸。 也許我不是在尋找要完成的正確術語。

我的問題是我已經在MS Excel加載項中編寫了一個函數,我按照Microsoft的說明作為起點,但是他們的教程在每次用戶保存文檔時都會執行代碼。 我的目標是在設計的功能區上有一個按鈕來執行此功能,而不是保存按鈕。

這是我入門時所遵循的Microsoft文章: https : //msdn.microsoft.com/zh-cn/library/cc668205.aspx

我也在這里找到了這個問題,但它沒有足夠的細節讓我自己找出如何實現該解決方案的方法: 如何將功能區按鈕連接到Excel加載項中定義的函數?

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
    }
}

提前感謝你的幫助。

通常,您可以使用Globals.ThisAddIn.Application來訪問應用程序級別和文檔級別的UI。 希望此鏈接能對您有所幫助。 這是向工作表中添加按鈕的示例,如下所示:

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

看起來像

VSTO提供了兩種創建自定義UI的方法:

  1. 功能區設計器-請參閱演練:使用功能區設計器創建自定義選項卡

  2. 原始XML標記- 演練:使用功能區XML創建自定義標簽

在這兩種情況下,您都可以使用Globals.ThisAddin屬性訪問外接程序屬性和方法,該屬性返回外接程序類的實例(如上面的代碼所示)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM