Share via


How to: Add Custom Menus and Menu Items to Outlook

This example creates a menu in Microsoft Office Outlook. The menu, which contains one item, appears at the top of the application. When you click the menu item, the code displays a message that shows the menu item caption.

Applies to: The information in this topic applies to application-level projects for Outlook 2007. For more information, see Features Available by Office Application and Project Type.

link to video For a related video demonstration, see How Do I: Customize Outlook Item Context Menus?.

Example

Private menuBar As Office.CommandBar
Private newMenuBar As Office.CommandBarPopup
Private buttonOne As Office.CommandBarButton


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

Private Sub AddMenuBar()
    Try
        menuBar = Me.Application.ActiveExplorer().CommandBars.ActiveMenuBar
        newMenuBar = menuBar.Controls.Add( _
            Office.MsoControlType.msoControlPopup, _
            Temporary:=True)
        If newMenuBar IsNot Nothing Then
            newMenuBar.Caption = "New Menu"
            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 Office.CommandBar menuBar;
private Office.CommandBarPopup newMenuBar;
private Office.CommandBarButton buttonOne;

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

private void AddMenuBar()
{
    try
    {
        menuBar = this.Application.ActiveExplorer().CommandBars.ActiveMenuBar;
        newMenuBar = (Office.CommandBarPopup)menuBar.Controls.Add(
            Office.MsoControlType.msoControlPopup, missing,
            missing, missing, true);
        if (newMenuBar != null)
        {
            newMenuBar.Caption = "New Menu";
            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)
    {
        System.Windows.Forms.MessageBox.Show(ex.Message);
    }
}

private void buttonOne_Click(Office.CommandBarButton ctrl,
    ref bool cancel)
{
    System.Windows.Forms.MessageBox.Show("You clicked: " + ctrl.Caption,
        "Custom Menu", System.Windows.Forms.MessageBoxButtons.OK);
}

Robust Programming

Declare your command bar variables at the class level. If you declare them inside a method, they will go out of scope as soon as the method finishes running and the garbage collector will be able to reallocate the memory.

See Also

Tasks

How to: Create Office Toolbars

How to: Create Office Toolbars

How to: Add Commands to Shortcut Menus in Excel

Other Resources

Outlook Object Model Overview

Office UI Customization

Designing and Creating Office Solutions

Change History

Date

History

Reason

May 2011

Simplified the code example.

Customer feedback.