방법: 프로그래밍 방식으로 Office 메뉴 만들기
업데이트: 2007년 11월
적용 대상 |
---|
이 항목의 정보는 지정된 Visual Studio Tools for Office 프로젝트 및 Microsoft Office 버전에만 적용됩니다. 프로젝트 형식
Microsoft Office 버전
자세한 내용은 응용 프로그램 및 프로젝트 형식에 따라 사용 가능한 기능을 참조하십시오. |
이 예제에서는 Microsoft Office Excel 2003의 메뉴 모음에 New Menu라는 메뉴를 만듭니다. 이 새 메뉴는 도움말 메뉴 앞에 배치됩니다. 여기에는 메뉴 명령이 하나 포함됩니다. 메뉴 명령을 클릭하면 Sheet1의 셀에 텍스트가 삽입됩니다.
Microsoft Office Word 2003의 사용자 인터페이스를 사용자 지정하는 방법에 대한 예제는 방법: 프로그래밍 방식으로 Office 도구 모음 만들기 및 연습: 책갈피에 대한 바로 가기 메뉴 만들기를 참조하십시오.
ThisWorkbook 클래스에 다음 코드를 추가합니다.
참고: |
---|
이벤트 처리기를 추가할 때 컨트롤의 Tag 속성을 설정해야 합니다. Office에서는 특정 CommandBarControl의 이벤트 처리기를 추적하기 위해 Tag 속성을 사용합니다. Tag 속성이 비어 있으면 이벤트가 올바르게 처리되지 않습니다. |
참고: |
---|
메뉴 변수가 호출되는 메서드 안이 아닌 클래스 수준에서 해당 변수를 선언합니다. 이렇게 하면 응용 프로그램이 실행되는 동안 메뉴 변수를 범위에 계속 유지할 수 있습니다. 그렇지 않으면 가비지 수집으로 항목이 제거되고 이벤트 처리기 코드 실행이 중단됩니다. |
예제
' Declare the menu variable at the class level.
Private WithEvents menuCommand As Office.CommandBarButton
Private menuTag As String = "A unique tag"
' Call AddMenu from the Startup event of ThisWorkbook.
Private Sub ThisWorkbook_Startup(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles Me.Startup
CheckIfMenuBarExists()
AddMenuBar()
End Sub
' If the menu already exists, remove it.
Private Sub CheckIfMenuBarExists()
Try
Dim foundMenu As Office.CommandBarPopup = _
Me.Application.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
' Create the menu, if it does not exist.
Private Sub AddMenuBar()
Try
Dim menuBar As Office.CommandBar = Application.comm.CommandBars.ActiveMenuBar
Dim menuCaption As String = "Ne&w Menu"
If menuBar IsNot Nothing Then
Dim cmdBarControl As Office.CommandBarPopup = Nothing
Dim controlCount As Integer = menuBar.Controls.Count
' Add the new menu.
cmdBarControl = CType(menuBar.Controls.Add( _
Type:=Office.MsoControlType.msoControlPopup, Before:=controlCount, Temporary:=True), _
Office.CommandBarPopup)
cmdBarControl.Caption = menuCaption
' Add the menu command.
menuCommand = CType(cmdBarControl.Controls.Add( _
Type:=Office.MsoControlType.msoControlButton, Temporary:=True), _
Office.CommandBarButton)
With menuCommand
.Caption = "&New Menu Command"
.Tag = "NewMenuCommand"
.FaceId = 65
End With
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
' Add text to cell A1 when the menu is clicked.
Private Sub menuCommand_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, _
ByRef CancelDefault As Boolean) Handles menuCommand.Click
Globals.Sheet1.Range("A1").Value2 = "The menu command was clicked."
End Sub
// Declare the menu variable at the class level.
private Office.CommandBarButton menuCommand;
private string menuTag = "A unique tag";
// Call AddMenu from the Startup event of ThisWorkbook.
private void ThisWorkbook_Startup(object sender, System.EventArgs e)
{
CheckIfMenuBarExists();
AddMenuBar();
}
// If the menu already exists, remove it.
private void CheckIfMenuBarExists()
{
try
{
Office.CommandBarPopup foundMenu = (Office.CommandBarPopup)
this.Application.CommandBars.ActiveMenuBar.FindControl(
Office.MsoControlType.msoControlPopup, System.Type.Missing, menuTag, true, true);
if (foundMenu != null)
{
foundMenu.Delete(true);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
// Create the menu, if it does not exist.
private void AddMenuBar()
{
try
{
Office.CommandBarPopup cmdBarControl = null;
Office.CommandBar menubar = (Office.CommandBar)Application.CommandBars.ActiveMenuBar;
int controlCount = menubar.Controls.Count;
string menuCaption = "&New Menu";
// Add the menu.
cmdBarControl = (Office.CommandBarPopup)menubar.Controls.Add(
Office.MsoControlType.msoControlPopup, missing, missing, controlCount, true);
if (cmdBarControl != null)
{
cmdBarControl.Caption = menuCaption;
// Add the menu command.
menuCommand = (Office.CommandBarButton)cmdBarControl.Controls.Add(
Office.MsoControlType.msoControlButton, missing, missing, missing, true);
menuCommand.Caption = "&New Menu Command";
menuCommand.Tag = "NewMenuCommand";
menuCommand.FaceId = 65;
menuCommand.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(
menuCommand_Click);
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
// Add text to cell A1 when the menu is clicked.
private void menuCommand_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)
{
Globals.Sheet1.Range["A1", missing].Value2 = "The menu command was clicked.";
}
참고 항목
작업
방법: 프로그래밍 방식으로 Office 도구 모음 만들기