最近看到很多朋友在问如何在office中添加新工具条
简单写了一点代码,供大家参考
我使用vs2003开发office addin
首先定义了几个东西,大家看看应该都能明白
public word.Application fkrl;
public object o = System.Reflection.Missing.Value ;
public CommandBars cm;
public CommandBar bar;
public CommandBarButton cm_begin;
public object o = System.Reflection.Missing.Value ;
public CommandBars cm;
public CommandBar bar;
public CommandBarButton cm_begin;
在OnStartupComplete中添加代码,添加一个新的工具条“CM”
fkrl=(word.Application)this.applicationObject;
cm = (CommandBars)applicationObject.GetType().InvokeMember("CommandBars", BindingFlags.GetProperty , null, applicationObject ,null);
try
{
bar=cm["CM"];
}
catch
{
bar = cm.Add("CM",o,o,o);
}
bar.Visible=true;
cm = (CommandBars)applicationObject.GetType().InvokeMember("CommandBars", BindingFlags.GetProperty , null, applicationObject ,null);
try
{
bar=cm["CM"];
}
catch
{
bar = cm.Add("CM",o,o,o);
}
bar.Visible=true;
然后在其中添加一个按钮
try
{
cm_begin = (CommandBarButton)bar.Controls["开启CM"];
}
catch(Exception)
{
cm_begin =(CommandBarButton)bar.Controls.Add(1,o,o,o,o);
cm_begin.Caption="开启CM";
cm_begin.Style=MsoButtonStyle.msoButtonCaption;
cm_begin.Visible=true;
}
cm_begin.Enabled=true;
cm_begin.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(this.cm_begin_Click);
{
cm_begin = (CommandBarButton)bar.Controls["开启CM"];
}
catch(Exception)
{
cm_begin =(CommandBarButton)bar.Controls.Add(1,o,o,o,o);
cm_begin.Caption="开启CM";
cm_begin.Style=MsoButtonStyle.msoButtonCaption;
cm_begin.Visible=true;
}
cm_begin.Enabled=true;
cm_begin.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(this.cm_begin_Click);
剩下的就是添加 cm_begin_Click 事件了,其实很简单的
还希望大家多多指教