首页 > 代码库 > VSTO Word2003 添加菜单栏, 添加工具栏
VSTO Word2003 添加菜单栏, 添加工具栏
直接上代码了:
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 添加菜单栏, 添加工具栏