无法在用户操作和Excel处理标准按钮点击事件之间挂钩
需要什么: 我需要一个Excel Addin,可以捕获功能区选项卡上各种标准命令栏按钮的点击事件。另外,完成此操作后,应进行命令按钮的正常处理。
示例: 当我捕获功能区按钮点击事件时,单击“保存”按钮时,我应该能够 a)进行自定义处理,显示消息框或记录此单击事件到平面文件和 工作表的实际工作
b)让Excel做保存我推荐的
- 在Office Fluent Ribbon上暂时重新调整命令
自定义开发人员的2007年Office Fluent Ribbon
,尽管可以捕获标准按钮点击am 无法获得两个点a)和b)同时工作。
ribbon1.xml
<?xml version="1.0" encoding="UTF-8"?>
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load">
<commands>
<command idMso="FileSave" onAction="OnRibbonButtonClick" />
</commands>
</customUI>
ribbon1.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using Office = Microsoft.Office.Core;
namespace ExcelAddIn
{
[ComVisible(true)]
public class Ribbon1 : Office.IRibbonExtensibility
{
private Office.IRibbonUI ribbon;
public Ribbon1()
{
}
#region IRibbonExtensibility Members
public string GetCustomUI(string ribbonID)
{
return GetResourceText("ExcelAddIn.Ribbon1.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;
}
public void OnRibbonButtonClick(Office.IRibbonControl control, bool Cancel)
//public void OnRibbonButtonClick(Office.IRibbonControl control)
{
Globals.ThisAddIn.OnStandardRibbonCommand(control.Id);
ribbon.Invalidate();
Cancel = false;
}
#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
}
}
带有上述源代码,我可以捕获FileSave事件,但该文件无法保存。即不会发生保存文件的默认excel功能。
我缺少某件事还是无法做到
? 这是否意味着命令已完全重新使用并且无法实现?
环境: Microsoft®Excel®Microsoft365 MSO(版本2205 Build 16.0.15225.20172)64位
Microsoft Visual Studio Enterprise 2019版本16.11.11.7
VSTO版本10.0.60724
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
重新使用的回调应具有以下签名:
似乎您需要使用以下方法:
The repurposed callback should have the following signature:
It seems you need to use the following method instead:
处理重新启动按钮点击事件后,您只需调用
worksheet.save
即可。或者,您可以避免重新启动,只需处理
WordSheet.beforesave
/afterSave
事件即可。After you process the repurposed button click event, you can simply call
Worksheet.Save
.Or you can avoid repurposing and just handle
Wordsheet.BeforeSave
/AfterSave
events.