提问人:Grant McConville 提问时间:11/10/2023 更新时间:11/10/2023 访问量:23
如何使用 VSTO 在 Excel 中获取自定义按钮
How to get custom buttons working in Excel with VSTO
问:
我正在尝试使用 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
}
}
任何帮助都非常感谢。
答:
0赞
Grant McConville
11/10/2023
#1
弄清楚
public void LowerTextButton_Click(Office.IRibbonControl control)
{
MessageBox.Show("Button pressed");
}
需要公开,并且需要 IRibbonControl
评论