最近看到很多朋友在問如何在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;
在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;
然後在其中添加一個按鈕
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_Click 事件了,其實很簡單的
還希望大家多多指教