1、如何用 VSTO 给 EXCEL 添加右键菜单项用 VSTO 操纵 EXCEL 使用 C#语言开发的 添加的右键菜单下面的确有“My Test”一项,但是点击却不能进入 OnCancelRelate_ClickEventHandler 函数 请问有谁知道为什么? 代码如下 / 删掉所有名为 DEL_SUB_ITEM_STR 的菜单项 Microsoft.Office.Core.CommandBar cellBar = Globals.ThisWorkbook.Application.CommandBars“cell“; Microsoft.Office.Core.CommandBarContr
2、ols 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.Comma
3、ndBarButton; 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.St
4、yle = Microsoft.Office.Core.MsoButtonStyle.msoButtonCaption; 这句不要估计就行了=以下是添加按钮,并且添加事件的实例,请 LZ 参考。 。 。 新建 excel-addin 程序。 1、添加引用 using Office = Microsoft.Office.Core;using System.Collections;2、声明对象 private Excel.Application AppExcel;private Office.CommandBarButton btnCalc;3、添加代码 /宿主启动时执行private void
5、ThisAddIn_Startup(object sender, System.EventArgs e)AppExcel = Application;AddToolBar();4、以下是 AddToolBar()方法的实现 /添加工具栏private void AddToolBar()Office.CommandBars bars = AppExcel.CommandBars;if (bars = null)return;foreach (Office.CommandBar bar in bars)if (bar.Name.ToLower().Trim() = “mzbar“)bar.Dele
6、te();Office.CommandBar mzBar = AppExcel.CommandBars.Add(“mzBar“, 1, null, null);if (mzBar != null)btnCalc = (Office.CommandBarButton)mzBar.Controls.Add(Office.MsoControlType.msoControlButton, missing, missing, 1, true);if (btnCalc != null)btnCalc.Caption = “计算(btnCalc.Style = Office.MsoButtonStyle.m
7、soButtonIconAndCaption;btnCalc.TooltipText = “计算“;btnCalc.FaceId = 3277;btnCalc.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(btnCalc_Click);mzBar.Visible = true;4、以下是 btnCalc_Click 事件的实现 void btnCalc_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)/你要做的事情=Microsoft.Office.Core.CommandBarControl newCtrl 不要将 newctrl 定义成局部变量,要定义成全局变量