使用VSTO创建嵌入EXCEL菜单
该段代码是一个创建菜单的例子。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using Microsoft.Office.Core; //需要引用这个命名空间,vsto与菜单相关的类在该命名空间中
using Microsoft.Office.Interop.Excel ;
//using System.Reflection;
//作者:ERIc
//日期:2009年5月
//功能:生产主系统菜单
namespace Excel.Common
{
public class ExcelBulidMainMenu
{
static Microsoft.Office.Core.CommandBar menuBar; //实例化一个菜单条,该对象为静态对象,这样每个实例化该类的WorkBook都可以引用同一个菜单实例.
static Microsoft.Office.Core.CommandBarPopup menuCustom; //实例化一个弹出菜单对象.
static Microsoft.Office.Core.CommandBarButton menu1; //实例化一个菜单项.
public void BulidMainMenu(Workbook _self)
{
menuBar = _self.Application.CommandBars.ActiveMenuBar;
menuCustom =
(Microsoft.Office.Core.CommandBarPopup)menuBar.Controls.Add
(Microsoft.Office.Core.MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing, System.Type.Missing, true);
menuCustom.Caption = "系统管理(&S)";
menu1 =
(Microsoft.Office.Core.CommandBarButton)menuCustom.Controls.Add
(Microsoft.Office.Core.MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, System.Type.Missing, true);
menu1.Caption = "登入系统(&E)";
menu1.Click +=
new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(menu1_Click); //托管菜单项的Click事件.
}
}
}
using Microsoft.Office.Interop.Excel ;
//using System.Reflection;
//作者:ERIc
//日期:2009年5月
//功能:生产主系统菜单
namespace Excel.Common
{
public class ExcelBulidMainMenu
{
static Microsoft.Office.Core.CommandBar menuBar; //实例化一个菜单条,该对象为静态对象,这样每个实例化该类的WorkBook都可以引用同一个菜单实例.
static Microsoft.Office.Core.CommandBarPopup menuCustom; //实例化一个弹出菜单对象.
static Microsoft.Office.Core.CommandBarButton menu1; //实例化一个菜单项.
public void BulidMainMenu(Workbook _self)
{
menuBar = _self.Application.CommandBars.ActiveMenuBar;
menuCustom =
(Microsoft.Office.Core.CommandBarPopup)menuBar.Controls.Add
(Microsoft.Office.Core.MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing, System.Type.Missing, true);
menuCustom.Caption = "系统管理(&S)";
menu1 =
(Microsoft.Office.Core.CommandBarButton)menuCustom.Controls.Add
(Microsoft.Office.Core.MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, System.Type.Missing, true);
menu1.Caption = "登入系统(&E)";
menu1.Click +=
new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(menu1_Click); //托管菜单项的Click事件.
}
}
}