關於使用c#在office建立工具條

來源:互聯網
上載者:User

最近看到很多朋友在問如何在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 事件了,其實很簡單的

還希望大家多多指教

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.