用VSTO给EXCEL添加右键菜单项.doc

上传人:李医生 文档编号:5657232 上传时间:2020-07-20 格式:DOC 页数:2 大小:16KB
返回 下载 相关 举报
用VSTO给EXCEL添加右键菜单项.doc_第1页
第1页 / 共2页
用VSTO给EXCEL添加右键菜单项.doc_第2页
第2页 / 共2页
亲,该文档总共2页,全部预览完了,如果喜欢就下载吧!
资源描述

《用VSTO给EXCEL添加右键菜单项.doc》由会员分享,可在线阅读,更多相关《用VSTO给EXCEL添加右键菜单项.doc(2页珍藏版)》请在三一文库上搜索。

1、如何用VSTO给EXCEL添加右键菜单项用VSTO操纵EXCEL 使用C#语言开发的 添加的右键菜单下面的确有“My Test”一项,但是点击却不能进入OnCancelRelate_ClickEventHandler函数 请问有谁知道为什么? 代码如下 / 删掉所有名为 DEL_SUB_ITEM_STR 的菜单项 Microsoft.Office.Core.CommandBar cellBar = Globals.ThisWorkbook.Application.CommandBarscell; Microsoft.Office.Core.CommandBarControls bars = c

2、ellBar.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;

3、 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.

4、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 ThisAddIn_Startup(object

5、 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.Delete(); Office.Command

6、Bar 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 = 计算(&C); btnCalc.Style = Office.MsoButtonStyle.msoButtonIconA

7、ndCaption; 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定义成局部变量,要定义成全局变量

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 科普知识


经营许可证编号:宁ICP备18001539号-1