如何使用 VSTO 在 Excel 中使用自定义按钮

问题描述 投票:0回答:1

我正在尝试使用 Visual Studio 创建 Excel 加载项,但是我无法使按钮正常工作。

我已经添加了这样的按钮...... ExcelToolsRibbon.xml

<?xml version="1.0" encoding="UTF-8"?>
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load">
  <ribbon>
    <tabs>
      <tab id="DropshipToolsTab" label="Dropship Tools" insertAfterMso="TabHome">
        <group id="TextGroup" label="Text">
            <button id="LowerTextButton" label="Lower" size="normal" onAction="LowerTextButton_Click" />
            <button id="ProperTextButton" label="Proper" size="normal" onAction="ProperTextButton_Click"/>
            <button id="UpperTextButton" label="Upper" size="normal" onAction="UpperTextButton_Click"/>
        </group>
      </tab>
    </tabs>
  </ribbon>
</customUI>

功能区加载正常,因为当 Excel 运行时我可以看到按钮,但是当我单击按钮时没有任何反应。

using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Office = Microsoft.Office.Core;

namespace ExcelTools
{
    [ComVisible(true)]
    public class ExcelToolsRibbon : Office.IRibbonExtensibility
    {
        private Office.IRibbonUI ribbon;

        public ExcelToolsRibbon()
        {
        }

        #region IRibbonExtensibility Members

        public string GetCustomUI(string ribbonID)
        {
            return GetResourceText("ExcelTools.ExcelToolsRibbon.xml");
        }

        #endregion

        #region Ribbon Callbacks
        //Create callback methods here. For more information about adding callback methods, visit https://go.microsoft.com/fwlink/?LinkID=271226

        public void Ribbon_Load(Office.IRibbonUI ribbonUI)
        {
            this.ribbon = ribbonUI;
        }
        private void LowerTextButton_Click(Office.IRibbonControl control)
        {
            MessageBox.Show("Button pressed");
        }

        private void ProperTextButton_Click()
        {
            MessageBox.Show("Button pressed");
        }

        private void UpperTextButton_Click()
        {
            MessageBox.Show("Button pressed");
        }
        #endregion

        #region Helpers

        private static string GetResourceText(string resourceName)
        {
            Assembly asm = Assembly.GetExecutingAssembly();
            string[] resourceNames = asm.GetManifestResourceNames();
            for (int i = 0; i < resourceNames.Length; ++i)
            {
                if (string.Compare(resourceName, resourceNames[i], StringComparison.OrdinalIgnoreCase) == 0)
                {
                    using (StreamReader resourceReader = new StreamReader(asm.GetManifestResourceStream(resourceNames[i])))
                    {
                        if (resourceReader != null)
                        {
                            return resourceReader.ReadToEnd();
                        }
                    }
                }
            }
            return null;
        }

        #endregion
    }
}

非常感谢任何帮助。

c# excel xml vsto ribbonx
1个回答
0
投票

弄清楚了

        public void LowerTextButton_Click(Office.IRibbonControl control)
        {
            MessageBox.Show("Button pressed");
        }

需要公开并且需要IRibbonControl

© www.soinside.com 2019 - 2024. All rights reserved.