共用方式為


HOW TO:建立 Office 工具列

這個範例會在 Microsoft Office Outlook 中建立工具列。 這個含有兩個按鈕的工具列會顯示在應用程式的頂端。 當您按一下按鈕時,程式碼就會顯示一則含有按鈕標題的訊息。

**適用於:**本主題中的資訊適用於下列應用程式的應用程式層級專案:InfoPath 2007、Outlook 2007、Project 2007 和 Visio 2007。如需詳細資訊,請參閱依 Office 應用程式和專案類型提供的功能

雖然這是 Outlook 專屬範例,但是程式碼中建立工具列的部分,還是能用來建立以上所列之任何應用程式的工具列。

範例

Dim newToolBar As Office.CommandBar
Dim firstButton As Office.CommandBarButton
Dim secondButton As Office.CommandBarButton
Dim selectExplorers As Outlook.Explorers

Private Sub ThisAddIn_Startup(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles Me.Startup
    selectExplorers = Me.Application.Explorers()
    AddHandler selectExplorers.NewExplorer, AddressOf _
        Me.NewExplorer_Event
    AddToolbar()
End Sub

Private Sub NewExplorer_Event(ByVal new_Explorer _
    As Outlook.Explorer)
    new_Explorer.Activate()
    newToolBar = Nothing
    Call Me.AddToolbar()
End Sub

Private Sub AddToolbar()

    Dim button_1 As Office.CommandBarButton
    Dim button_2 As Office.CommandBarButton
    If newToolBar Is Nothing Then
        Dim cmdBars As Office.CommandBars = _
            Me.Application.ActiveExplorer().CommandBars
        newToolBar = cmdBars.Add("NewToolBar", _
            Office.MsoBarPosition.msoBarTop, False, True)
    End If
    Try
        button_1 = CType(newToolBar.Controls.Add(1),  _
            Office.CommandBarButton)
        With button_1
            .Style = Office.MsoButtonStyle.msoButtonCaption
            .Caption = "Button 1"
            .Tag = "Button1"
        End With
        If Me.firstButton Is Nothing Then
            Me.firstButton = button_1
            AddHandler firstButton.Click, AddressOf ButtonClick
        End If
        button_2 = CType(newToolBar.Controls.Add(1),  _
            Office.CommandBarButton)
        With button_2
            .Style = Office.MsoButtonStyle.msoButtonCaption
            .Caption = "Button 2"
            .Tag = "Button2"
        End With
        If Me.secondButton Is Nothing Then
            Me.secondButton = button_2
            AddHandler secondButton.Click, AddressOf ButtonClick
        End If
        newToolBar.Visible = True
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

Private Sub ButtonClick(ByVal ctrl As Office.CommandBarButton, _
        ByRef Cancel As Boolean)
    MsgBox("You clicked: " + ctrl.Caption)
End Sub
Office.CommandBar newToolBar;
Office.CommandBarButton firstButton;
Office.CommandBarButton secondButton;
Outlook.Explorers selectExplorers;

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{

    selectExplorers = this.Application.Explorers;
    selectExplorers.NewExplorer += new Outlook
        .ExplorersEvents_NewExplorerEventHandler(newExplorer_Event);
    AddToolbar();
}

private void newExplorer_Event(Outlook.Explorer new_Explorer)
{
    ((Outlook._Explorer)new_Explorer).Activate();
    newToolBar = null;
    AddToolbar();
}

private void AddToolbar()
{

    if (newToolBar == null)
    {
        Office.CommandBars cmdBars =
            this.Application.ActiveExplorer().CommandBars;
        newToolBar = cmdBars.Add("NewToolBar",
            Office.MsoBarPosition.msoBarTop, false, true);
    }
    try
    {
        Office.CommandBarButton button_1 =
            (Office.CommandBarButton)newToolBar.Controls
            .Add(1, missing, missing, missing, missing);
        button_1.Style = Office
            .MsoButtonStyle.msoButtonCaption;
        button_1.Caption = "Button 1";
        button_1.Tag = "Button1";
        if (this.firstButton == null)
        {
            this.firstButton = button_1;
            firstButton.Click += new Office.
                _CommandBarButtonEvents_ClickEventHandler
                (ButtonClick);
        }

        Office.CommandBarButton button_2 = (Office
            .CommandBarButton)newToolBar.Controls.Add
            (1, missing, missing, missing, missing);
        button_2.Style = Office
            .MsoButtonStyle.msoButtonCaption;
        button_2.Caption = "Button 2";
        button_2.Tag = "Button2";
        newToolBar.Visible = true;
        if (this.secondButton == null)
        {
            this.secondButton = button_2;
            secondButton.Click += new Office.
                _CommandBarButtonEvents_ClickEventHandler
                (ButtonClick);
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

private void ButtonClick(Office.CommandBarButton ctrl,
        ref bool cancel)
{
    MessageBox.Show("You clicked: " + ctrl.Caption);
}

穩固程式設計

請在類別層級宣告您的命令列變數,不要在呼叫變數的方法內進行宣告。 如此便可確保在應用程式執行期間,命令列變數都會保留在範圍內。 否則,記憶體回收會移除項目,而且您的事件處理常式不會執行。

請參閱

工作

HOW TO:將命令加入到 Excel 的捷徑功能表

HOW TO:將自訂功能表和功能表項目加入至 Outlook

逐步解說:建立書籤的快速鍵功能表

概念

Office 方案中的選擇性參數

其他資源

Office UI 自訂