当前位置: 代码迷 >> VBA >> 怎么用VSTO给EXCEL添加右键菜单项
  详细解决方案

怎么用VSTO给EXCEL添加右键菜单项

热度:2394   发布时间:2013-02-26 00:00:00.0
如何用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.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;
  相关解决方案