HOW TO:以程式設計方式建立 Office 功能表
更新:2007 年 11 月
適用於 |
---|
本主題中的資訊僅適用於指定的 Visual Studio Tools for Office 專案和 Microsoft Office 版本。 專案類型
Microsoft Office 版本
如需詳細資訊,請參閱依應用程式和專案類型提供的功能。 |
這個範例會在 Microsoft Office Excel 2003 的功能表列建立名為 New Menu 的功能表。這個新功能表位於 [說明] 功能表之前,而且它含有一個功能表命令。按一下該功能表命令時,會將文字插入 Sheet1 上的儲存格。
如需如何在 Microsoft Office Word 2003 中自訂使用者介面的範例,請參閱 HOW TO:以程式設計方式建立 Office 工具列和逐步解說:建立書籤的快速鍵功能表。
將下列程式碼加入 ThisWorkbook 類別。
注意事項: |
---|
加入事件處理常式時,您必須設定控制項的 Tag 屬性。Office 使用 Tag 屬性,追蹤特定 CommandBarControl 的事件處理常式。如果 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.";
}