Compartilhar via


Como: Adicionar menus Personalizars e itens de menu ao Outlook

Se aplica a

As informações contidas neste tópico se aplicam apenas às especificado Ferramentas do Visual Studio para o Office projetos e as versões do Microsoft Office.

Tipo de Projeto

  • Projetos de nível de aplicativo

Versão do Microsoft Office

  • Outlook 2003

  • Outlook 2007

For more information, see Recursos disponíveis pelo aplicativo e o tipo de projeto.

Este exemplo cria um menu no Microsoft Office Outlook.O menu, que contém um item, aparece na parte superior do aplicativo.Quando você Clique o item de menu, o código exibe uma mensagem que mostra a legenda do item de menu.

Exemplo

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
        MessageBox.Show(Ex.Message)
    End Try
End Sub

Public Sub ButtonOne_Click(ByVal buttonControl As Office. _
CommandBarButton, ByRef Cancel As Boolean)
    MessageBox.Show("You clicked: " & buttonControl.Caption, _
        "Custom Menu", MessageBoxButtons.OK)
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
        MessageBox.Show(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);
    }
}

Programação robusta

Declare seus variáveis de barra de comando no nível de classe.Se você declare-los dentro de um método, eles irão fora do escopo assim que o método terminar Executando e o coletor de lixo será capaz de realocar a memória.

Consulte também

Tarefas

Como: Adicionar barras de ferramentas Personalizars e itens de barra de ferramentas ao Outlook

Como: Criar barras de ferramentas do Office por programação

Como: Criar menus do Office por programação

Conceitos

Visão geral do modelo de objeto do Outlook

Personalização de IU do Office

Criar soluções do Office no Visual Studio