共用方式為


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

更新:2011 年 5 月

這個範例會在 Microsoft Office Outlook 中建立功能表。 這個含有一個項目的功能表會顯示在應用程式的頂端。 當您按一下功能表項目時,程式碼就會顯示一則含有功能表項目標題的訊息。

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

視訊的連結 如需觀看相關示範影片,請參閱如何:自訂 Outlook 項目內容功能表?(英文)。

範例

Private menuBar As Office.CommandBar
Private newMenuBar As Office.CommandBarPopup
Private buttonOne As Office.CommandBarButton
Private menuTag As String = "A unique tag"

Private Sub ThisApplication_Startup(ByVal sender As Object, ByVal e _
    As System.EventArgs) Handles Me.Startup
    RemoveMenubar()
    AddMenuBar()
End Sub

Private Sub AddMenuBar()
    Try
        menuBar = Me.Application.ActiveExplorer().CommandBars.ActiveMenuBar
        newMenuBar = menuBar.Controls.Add( _
            Office.MsoControlType.msoControlPopup, _
            Temporary:=False)
        If newMenuBar IsNot Nothing Then
            newMenuBar.Caption = "New Menu"
            newMenuBar.Tag = menuTag
            buttonOne = newMenuBar.Controls.Add( _
                Office.MsoControlType.msoControlButton, _
                Before:=1, Temporary:=True)

            With buttonOne
                .Style = Office.MsoButtonStyle.msoButtonIconAndCaption
                .Caption = "Button One"
                .FaceId = 65
                .Tag = "c123"
            End With

            AddHandler buttonOne.Click, AddressOf ButtonOne_Click
            newMenuBar.Visible = True
        End If
    Catch Ex As Exception
        MsgBox(Ex.Message)
    End Try
End Sub

Public Sub ButtonOne_Click(ByVal buttonControl As Office. _
CommandBarButton, ByRef Cancel As Boolean)
    MsgBox("You clicked: " & buttonControl.Caption, _
        "Custom Menu", vbOK)
End Sub

Private Sub RemoveMenubar()
    Try
        ' If the menu already exists, remove it.
        Dim foundMenu As Office.CommandBarPopup = _
            Me.Application.ActiveExplorer().CommandBars.ActiveMenuBar. _
            FindControl(Office.MsoControlType.msoControlPopup, _
            System.Type.Missing, menuTag, True, True)
        If foundMenu IsNot Nothing Then
            foundMenu.Delete(True)
        End If
    Catch Ex As Exception
        MsgBox(Ex.Message)
    End Try
End Sub
private Office.CommandBar menuBar;
private Office.CommandBarPopup newMenuBar;
private Office.CommandBarButton buttonOne;
private string menuTag = "A unique tag";

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    RemoveMenubar();
    AddMenuBar();
}

private void AddMenuBar()
{
    try
    {
        menuBar = this.Application.ActiveExplorer().CommandBars.ActiveMenuBar;
        newMenuBar = (Office.CommandBarPopup)menuBar.Controls.Add(
            Office.MsoControlType.msoControlPopup, missing,
            missing, missing, false);
        if (newMenuBar != null)
        {
            newMenuBar.Caption = "New Menu";
            newMenuBar.Tag = menuTag;
            buttonOne = (Office.CommandBarButton)newMenuBar.Controls.
            Add(Office.MsoControlType.msoControlButton, missing,
                missing, 1, true);
            buttonOne.Style = Office.MsoButtonStyle.
                msoButtonIconAndCaption;
            buttonOne.Caption = "Button One";
            buttonOne.FaceId = 65;
            buttonOne.Tag = "c123";
            buttonOne.Click += new
                Office._CommandBarButtonEvents_ClickEventHandler(
                buttonOne_Click);
            newMenuBar.Visible = true;
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

private void buttonOne_Click(Office.CommandBarButton ctrl,
    ref bool cancel)
{
    MessageBox.Show("You clicked: " + ctrl.Caption,
        "Custom Menu", MessageBoxButtons.OK);
}
private void RemoveMenubar()
{
    // If the menu already exists, remove it.
    try
    {
        Office.CommandBarPopup foundMenu = (Office.CommandBarPopup)
            this.Application.ActiveExplorer().CommandBars.ActiveMenuBar.
            FindControl(Office.MsoControlType.msoControlPopup,
            missing, menuTag, true, true);
        if (foundMenu != null)
        {
            foundMenu.Delete(true);
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

穩固程式設計

請在類別層級宣告命令列變數。 如果您在方法內部宣告這些變數,它們就會在方法執行完成時離開範圍,而且記憶體回收行程將能夠重新配置記憶體。

請參閱

工作

HOW TO:建立 Office 工具列

HOW TO:建立 Office 工具列

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

其他資源

Outlook 物件模型概觀

Office UI 自訂

設計和建立 Office 方案

變更記錄

日期

記錄

原因

2011 年 5 月

簡化程式碼範例。

客戶回函。