방법: Outlook에 사용자 지정 메뉴 및 메뉴 항목 추가

업데이트: 2011년 5월

이 예제에서는 Microsoft Office Outlook에 메뉴를 만듭니다. 한 개의 항목이 있는 이 메뉴는 응용 프로그램 위쪽에 표시됩니다. 메뉴 항목을 클릭하면 메뉴 항목 캡션을 표시하는 메시지가 표시됩니다.

적용 대상: 이 항목의 정보는 Outlook 2007의 응용 프로그램 수준 프로젝트에 적용됩니다. 자세한 내용은 Office 응용 프로그램 및 프로젝트 형식에 따라 사용 가능한 기능을 참조하십시오.

비디오에 링크 관련 비디오 데모를 보려면 How Do I: Customize Outlook Item Context Menus?를 참조하십시오.

예제

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

강력한 프로그래밍

클래스 수준에서 명령 모음 변수를 선언합니다. 명령 모음 변수를 메서드 안에 선언하면 메서드가 실행을 마치는 즉시 범위를 벗어나게 되고 가비지 수집기가 메모리를 다시 할당할 수 있게 됩니다.

참고 항목

작업

방법: Office 도구 모음 만들기

방법: Office 도구 모음 만들기

방법: Excel에서 바로 가기 메뉴에 명령 추가

기타 리소스

Outlook 개체 모델 개요

Office UI 사용자 지정

Office 솔루션 디자인 및 만들기

변경 기록

날짜

변경 내용

이유

2011년 5월

코드 예제를 단순화했습니다.

고객 의견