用VSTO操纵EXCEL 使用C#语言开发的 添加的右键菜单下面的确有“My Test”一项,
但是点击却不能进入OnCancelRelate_ClickEventHandler函数
请问有谁知道为什么??
代码如下
// 删掉所有名为 DEL_SUB_ITEM_STR 的菜单项
Microsoft.Office.Core.CommandBar cellBar = Globals.ThisWorkbook.Application.CommandBars["cell"];
Microsoft.Office.Core.CommandBarControls bars = cellBar.Controls;
Microsoft.Office.Core.CommandBarControl newCtrl = bars.Add(Microsoft.Office.Core.MsoControlType.msoControlButton, missing, missing, missing, true);
//newCtrl.Caption = DEL_SUB_ITEM_STR;
Microsoft.Office.Core.CommandBarButton newBtn = newCtrl as Microsoft.Office.Core.CommandBarButton;
newBtn.Tag = "MyTest";
newBtn.Caption = "My Test";
newBtn.FaceId = 0163;
newBtn.Style = Microsoft.Office.Core.MsoButtonStyle.msoButtonCaption;
newBtn.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(OnCancelRelate_ClickEventHandler);
------解决方案--------------------------------------------------------
检查你添加的右键菜单项的类型
------解决方案--------------------------------------------------------
newBtn.Style = Microsoft.Office.Core.MsoButtonStyle.msoButtonCaption;
这句不要估计就行了
------解决方案--------------------------------------------------------
呵呵。。。以下是添加按钮,并且添加事件的实例,请LZ参考。。。
新建excel-addin程序。
1、添加引用
- C# code
using Office = Microsoft.Office.Core;using System.Collections;
------解决方案--------------------------------------------------------
不好意思,忘记添加了一个命名空间
- C# code
using Excel = Microsoft.Office.Interop.Excel;