다음을 통해 공유


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

업데이트: 2007년 11월

적용 대상

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

프로젝트 형식

  • 문서 수준 프로젝트

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

Microsoft Office 버전

  • Microsoft Office 2003

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

이 예제에서는 Microsoft Office Word 2003에서 Test라는 도구 모음을 만듭니다. 이 도구 모음은 문서의 중간 정도에 표시되고 두 개의 단추를 포함합니다. 단추를 클릭하면 메시지 상자가 나타납니다. Microsoft Office Excel 2003에서 사용자 인터페이스를 사용자 지정하는 방법에 대한 예제는 방법: 프로그래밍 방식으로 Office 메뉴 만들기를 참조하십시오.

ThisDocument 클래스에 다음 코드를 추가합니다.

참고:

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

예제

' Create the command bar variables at the class level.
Dim commandBar As Office.CommandBar
Dim firstButton As Office.CommandBarButton
Dim secondButton As Office.CommandBarButton


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

    AddToolbar()
End Sub


Private Sub AddToolbar()

    Try
        commandBar = Me.CommandBars("Test")
    Catch ex As ArgumentException
        ' Toolbar named Test does not exist so we should create it.
    End Try

    If commandBar Is Nothing Then
        commandBar = Application.CommandBars.Add("Test", 1, False, True)
    End If

    Try
        ' Add a button to the command bar and create an event handler.
        firstButton = CType(commandBar.Controls.Add(1), Office.CommandBarButton)

        firstButton.Style = Office.MsoButtonStyle.msoButtonCaption
        firstButton.Caption = "button 1"
        firstButton.Tag = "button1"
        AddHandler firstButton.Click, AddressOf ButtonClick

        ' Add a second button to the command bar and create an event handler.
        secondButton = CType(commandBar.Controls.Add(1), Office.CommandBarButton)

        secondButton.Style = Office.MsoButtonStyle.msoButtonCaption
        secondButton.Caption = "button 2"
        secondButton.Tag = "button2"
        AddHandler secondButton.Click, AddressOf ButtonClick

        commandBar.Visible = True

    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub


' Handles the event when a button on the new toolbar is clicked.
Private Sub ButtonClick(ByVal ctrl As Office.CommandBarButton, ByRef Cancel As Boolean)

    MsgBox("You clicked: " & ctrl.Caption)
End Sub
// Create the command bar variables at the class level.
Office.CommandBar commandBar;
Office.CommandBarButton firstButton;
Office.CommandBarButton secondButton;

private void ThisDocument_Startup(object sender, System.EventArgs e)
{
    AddToolbar();
}

private void AddToolbar()
{
    try
    {
        commandBar = Application.CommandBars["Test"];
    }
    catch (ArgumentException e)
    {
        // Toolbar named Test does not exist so we should create it.
    }

    if (commandBar == null)
    {
        // Add a commandbar named Test.
        commandBar = Application.CommandBars.Add("Test", 1, missing, true);
    }

    try
    {
        // Add a button to the command bar and an event handler.
        firstButton = (Office.CommandBarButton)commandBar.Controls.Add(
            1, missing, missing, missing, missing);

        firstButton.Style = Office.MsoButtonStyle.msoButtonCaption;
        firstButton.Caption = "button 1";
        firstButton.Tag = "button1";
        firstButton.Click += new Office._CommandBarButtonEvents_ClickEventHandler(ButtonClick);

        // Add a second button to the command bar and an event handler.
        secondButton = (Office.CommandBarButton)commandBar.Controls.Add(
            1, missing, missing, missing, missing);

        secondButton.Style = Office.MsoButtonStyle.msoButtonCaption;
        secondButton.Caption = "button 2";
        secondButton.Tag = "button2";
        secondButton.Click += new Office._CommandBarButtonEvents_ClickEventHandler(ButtonClick);

        commandBar.Visible = true;
    }
    catch (ArgumentException e)
    {
        MessageBox.Show(e.Message);
    }
}

// Handles the event when a button on the new toolbar is clicked.
private void ButtonClick(Office.CommandBarButton ctrl, ref bool cancel)
{
    MessageBox.Show("You clicked: " + ctrl.Caption);
}

참고 항목

작업

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

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

개념

Office UI 사용자 지정

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