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

업데이트: 2007년 11월

적용 대상

이 항목의 정보는 지정된 Visual Studio Tools for Office 프로젝트 및 Microsoft Office 버전에만 적용됩니다.

프로젝트 형식

  • 응용 프로그램 수준 프로젝트

Microsoft Office 버전

  • Outlook 2003

  • Outlook 2007

자세한 내용은 응용 프로그램 및 프로젝트 형식에 따라 사용 가능한 기능을 참조하십시오.

이 예제에서는 Microsoft 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
        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);
    }
}

강력한 프로그래밍

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

참고 항목

작업

방법: Outlook에 사용자 지정 도구 모음 및 도구 모음 항목 추가

방법: 프로그래밍 방식으로 Office 도구 모음 만들기

방법: 프로그래밍 방식으로 Office 메뉴 만들기

개념

Outlook 개체 모델 개요

Office UI 사용자 지정

Visual Studio에서 Office 솔루션 만들기