VSTO Word2003 添加菜单栏, 添加工具栏
来源:互联网 发布:python numpy 编辑:程序博客网 时间:2024/05/21 10:36
直接上代码了:
Microsoft.Office.Core.CommandBar menuBar;
CommandBarButton ccbtn = null;
CommandBarButton btnRequirementProperty;
CommandBarButton btnCancelImport;
CommandBarButton btnCancelImport ;
Office.CommandBarButton btn ;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
//this.AddMenu();
//return;Word.Application wordApp = this.Application;
wordApp.Visible = true;
//Microsoft.Office.Interop.Word.Application Application.
// this.Application = this.GetHostItem<Microsoft.Office.Interop.Word.Application>(typeof(Microsoft.Office.Interop.Word.Application), "Application");
//this.Application.ActiveDocument
//Create a Command Bar
object missing = System.Reflection.Missing.Value;
menuBar = ((Microsoft.Office.Interop.Word.Application)wordApp).CommandBars.ActiveMenuBar;// ["Menu Bar"];
//CommandBar toolBar = wordApp.CommandBars["Menu Bar"];//删除菜单上次生成的菜单.
Microsoft.Office.Core.CommandBarControls bars = menuBar.Controls;
foreach (Microsoft.Office.Core.CommandBarControl temp_contrl in bars)
{
string t = temp_contrl.Tag;
//如果已经存在就删除
if (t == "导入")
{
temp_contrl.Delete(false);
}
}
menuBar.Visible = true;
//添加菜单栏 菜单
// 一级菜单
//
Microsoft.Office.Core.CommandBarPopup popuBar =
(Microsoft.Office.Core.CommandBarPopup)menuBar.Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlPopup,
missing, missing, missing, true);
popuBar.Tag = "导入";
popuBar.Caption = "导入";
popuBar.Visible = true;
// 二级菜单
btnRequirementProperty =
(CommandBarButton)popuBar.Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlButton, 1, "", 1, true);
btnRequirementProperty.Caption = "单条导入";
btnRequirementProperty.Visible = true;
btnRequirementProperty.Style = MsoButtonStyle.msoButtonIconAndCaption;
btnRequirementProperty.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(this.btn_Click);btnCancelImport =
(CommandBarButton)popuBar.Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlButton, 1, "", 1, true);
btnCancelImport.Caption = "批量导入";
btnCancelImport.Visible = true;
btnCancelImport.Style = MsoButtonStyle.msoButtonCaption;
btnCancelImport.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(this.btn_Click);
//添加工具栏 工具
Office.CommandBar commandBar = wordApp.CommandBars.Add("My Bar", Office.MsoBarPosition.msoBarTop, false, true);
commandBar.Visible = true;////Add a Command Bar button
btn = commandBar.Controls.Add(Office.MsoControlType.msoControlButton,
missing, missing, missing, true) as Office.CommandBarButton;
btn.Caption = "My 233332";
btn.Tag = "MyButton";
btn.Style = MsoButtonStyle.msoButtonCaption;
btn.Click += new _CommandBarButtonEvents_ClickEventHandler(btn_Click);
}void btn_Click(CommandBarButton Ctrl, ref bool CancelDefault)
{
MessageBox.Show("My button is clicked!" + DateTime.Now);
}
出现的问题及解决方案:
1. 每次执行菜单都会重新添加菜单. 我这里的解决先删除之前的菜单, 重新添加菜单
Microsoft.Office.Core.CommandBarControls bars = menuBar.Controls;
foreach (Microsoft.Office.Core.CommandBarControl temp_contrl in bars)
{
string t = temp_contrl.Tag;
//如果已经存在就删除
if (t == "导入")
{
temp_contrl.Delete(false);
}
}
2. 添加菜单及工具栏的事件只能执行一次. 第二次就没反应了. 将按钮的变量设置成类的成员变量. 不能放在方法内
CommandBarButton btnRequirementProperty;
CommandBarButton btnCancelImport;
Office.CommandBarButton btn ;
- VSTO Word2003 添加菜单栏, 添加工具栏
- vs2010添加菜单栏和工具栏
- MFC框体添加菜单栏工具栏状态栏
- MFC框体添加菜单栏工具栏状态栏
- MFC框体添加菜单栏工具栏状态栏
- 多文档 添加菜单栏 工具栏 删除ribbon
- 添加菜单栏、工具栏按钮、状态栏,并将菜单栏和工具栏关联
- Qt mainWindow添加状态栏,菜单栏,工具栏的Action方法
- C#Windows窗体界面设计_05_添加菜单栏 工具栏 状态栏 按钮
- Qt mainWindow添加状态栏,菜单栏,工具栏的Action方法
- 使用Qt designer 向菜单栏和工具栏添加action
- 添加工具栏
- 添加工具栏
- 添加工具栏
- word2003字体类型添加方法
- VSTO 添加Word右键菜单
- 添加工具栏和工具栏按钮添加图片
- 在 Excel 中自定义菜单栏和工具栏-添加、删除、禁止等
- 爬虫第四课(RegEx爬取新闻网站)
- Java关键字final、static使用
- 【hdu 1087】 Super Jumping! Jumping! Jumping! (LIS变形)
- 动态规划之找零钱问题与背包问题
- Delphi Dataset CurValue
- VSTO Word2003 添加菜单栏, 添加工具栏
- Verilog HDL程序设计——基本要素
- 2002-新老身份证号码比较
- gsoap工具生成wsdl接口 注意事项
- 单片机复位电路原理介绍
- Android开发的经典入门教材和学习路线
- NKOJ 1979 投票
- 小 X的密室
- TensorFlow中常见的由于API版本问题导致的错误汇总