Gewusst wie: Hinzufügen von benutzerdefinierten Menüs und Menüelementen zu Outlook

In diesem Beispiel wird ein Menü in Microsoft Office Outlook erstellt. Das Menü, das ein Element enthält, wird am oberen Rand der Anwendung angezeigt. Wenn Sie auf das Menüelement klicken, zeigt der Code eine Meldung mit der Menüelementbeschriftung an.

Betrifft: Die Informationen in diesem Thema betreffen Projekte auf Anwendungsebene für Outlook 2007. Weitere Informationen finden Sie unter Verfügbare Funktionen nach Office-Anwendung und Projekttyp.

Link zu Video Eine entsprechende Videodemo finden Sie unter How Do I: Customize Outlook Item Context Menus?.

Beispiel

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);
    }
}

Robuste Programmierung

Deklarieren Sie die Befehlsleistenvariablen auf Klassenebene. Beim Deklarieren innerhalb einer Methode verlassen die Variablen den Gültigkeitsbereich, sobald die Ausführung der Methode abgeschlossen ist, und der Garbage Collector kann den Arbeitsspeicher neu belegen.

Siehe auch

Aufgaben

Gewusst wie: Erstellen von Office-Symbolleisten

Gewusst wie: Erstellen von Office-Symbolleisten

Gewusste wie: Hinzufügen von Befehlen zu Kontextmenüs in Excel

Weitere Ressourcen

Übersicht über das Outlook-Objektmodell

Anpassung der Office-Benutzeroberfläche

Entwerfen und Erstellen von Office-Lösungen

Änderungsprotokoll

Datum

Versionsgeschichte

Grund

Mai 2011

Das Codebeispiel wurde vereinfacht.

Kundenfeedback.