방법: Office 도구 모음 만들기

이 예제에서는 Microsoft Office Outlook에 도구 모음을 만듭니다. 두 개의 단추가 있는 이 도구 모음은 응용 프로그램 위쪽에 표시됩니다. 단추를 클릭하면 단추 제목을 표시하는 메시지가 표시됩니다.

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

이 예제는 Outlook과 관련된 내용이지만 도구 모음을 만드는 이 코드 부분은 위에 나열된 모든 응용 프로그램에서 도구 모음을 만드는 데 사용할 수 있습니다.

예제

Dim newToolBar As Office.CommandBar
Dim firstButton As Office.CommandBarButton
Dim secondButton As Office.CommandBarButton
Dim selectExplorers As Outlook.Explorers

Private Sub ThisAddIn_Startup(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles Me.Startup
    selectExplorers = Me.Application.Explorers()
    AddHandler selectExplorers.NewExplorer, AddressOf _
        Me.NewExplorer_Event
    AddToolbar()
End Sub

Private Sub NewExplorer_Event(ByVal new_Explorer _
    As Outlook.Explorer)
    new_Explorer.Activate()
    newToolBar = Nothing
    Call Me.AddToolbar()
End Sub

Private Sub AddToolbar()

    Dim button_1 As Office.CommandBarButton
    Dim button_2 As Office.CommandBarButton
    If newToolBar Is Nothing Then
        Dim cmdBars As Office.CommandBars = _
            Me.Application.ActiveExplorer().CommandBars
        newToolBar = cmdBars.Add("NewToolBar", _
            Office.MsoBarPosition.msoBarTop, False, True)
    End If
    Try
        button_1 = CType(newToolBar.Controls.Add(1),  _
            Office.CommandBarButton)
        With button_1
            .Style = Office.MsoButtonStyle.msoButtonCaption
            .Caption = "Button 1"
            .Tag = "Button1"
        End With
        If Me.firstButton Is Nothing Then
            Me.firstButton = button_1
            AddHandler firstButton.Click, AddressOf ButtonClick
        End If
        button_2 = CType(newToolBar.Controls.Add(1),  _
            Office.CommandBarButton)
        With button_2
            .Style = Office.MsoButtonStyle.msoButtonCaption
            .Caption = "Button 2"
            .Tag = "Button2"
        End With
        If Me.secondButton Is Nothing Then
            Me.secondButton = button_2
            AddHandler secondButton.Click, AddressOf ButtonClick
        End If
        newToolBar.Visible = True
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

Private Sub ButtonClick(ByVal ctrl As Office.CommandBarButton, _
        ByRef Cancel As Boolean)
    MsgBox("You clicked: " + ctrl.Caption)
End Sub
Office.CommandBar newToolBar;
Office.CommandBarButton firstButton;
Office.CommandBarButton secondButton;
Outlook.Explorers selectExplorers;

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

    selectExplorers = this.Application.Explorers;
    selectExplorers.NewExplorer += new Outlook
        .ExplorersEvents_NewExplorerEventHandler(newExplorer_Event);
    AddToolbar();
}

private void newExplorer_Event(Outlook.Explorer new_Explorer)
{
    ((Outlook._Explorer)new_Explorer).Activate();
    newToolBar = null;
    AddToolbar();
}

private void AddToolbar()
{

    if (newToolBar == null)
    {
        Office.CommandBars cmdBars =
            this.Application.ActiveExplorer().CommandBars;
        newToolBar = cmdBars.Add("NewToolBar",
            Office.MsoBarPosition.msoBarTop, false, true);
    }
    try
    {
        Office.CommandBarButton button_1 =
            (Office.CommandBarButton)newToolBar.Controls
            .Add(1, missing, missing, missing, missing);
        button_1.Style = Office
            .MsoButtonStyle.msoButtonCaption;
        button_1.Caption = "Button 1";
        button_1.Tag = "Button1";
        if (this.firstButton == null)
        {
            this.firstButton = button_1;
            firstButton.Click += new Office.
                _CommandBarButtonEvents_ClickEventHandler
                (ButtonClick);
        }

        Office.CommandBarButton button_2 = (Office
            .CommandBarButton)newToolBar.Controls.Add
            (1, missing, missing, missing, missing);
        button_2.Style = Office
            .MsoButtonStyle.msoButtonCaption;
        button_2.Caption = "Button 2";
        button_2.Tag = "Button2";
        newToolBar.Visible = true;
        if (this.secondButton == null)
        {
            this.secondButton = button_2;
            secondButton.Click += new Office.
                _CommandBarButtonEvents_ClickEventHandler
                (ButtonClick);
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

private void ButtonClick(Office.CommandBarButton ctrl,
        ref bool cancel)
{
    MessageBox.Show("You clicked: " + ctrl.Caption);
}

강력한 프로그래밍

명령 모음 변수가 호출되는 메서드 안이 아닌 클래스 수준에서 해당 변수를 선언합니다. 이렇게 하면 응용 프로그램이 실행되는 동안 명령 모음 변수를 범위에 계속 유지할 수 있습니다. 그렇지 않으면 가비지 수집으로 항목이 제거되고 이벤트 처리기 코드가 실행되지 않습니다.

참고 항목

작업

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

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

연습: 책갈피에 대한 바로 가기 메뉴 만들기

개념

Office 솔루션의 선택적 매개 변수

기타 리소스

Office UI 사용자 지정