//添加“物品表”菜单命令 materialCommand = (Office.CommandBarButton)cmdBarControl.Controls.Add(Office.MsoControlType.msoControlButton, missing, missing, missing, true); materialCommand.Caption = "物品表(&M)"; materialCommand.Tag = "materialCommand"; materialCommand.FaceId = 0162; materialCommand.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(materialCommand_Click); //添加“申购单”菜单命令 questBuyBillCommand = (Office.CommandBarButton)cmdBarControl.Controls.Add(Office.MsoControlType.msoControlButton, missing, missing, missing, true); questBuyBillCommand.Caption = "申购单输入(&Q)"; questBuyBillCommand.Tag = "questBuyBillCommand"; questBuyBillCommand.FaceId = 0162; questBuyBillCommand.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(questBuyBillCommand_Click); //添加“报价单”菜单命令 quotationCommand = (Office.CommandBarButton)cmdBarControl.Controls.Add(Office.MsoControlType.msoControlButton, missing, missing, missing, true); quotationCommand.Caption = "报价单输入(&U)"; quotationCommand.Tag = "quotationCommand"; quotationCommand.FaceId = 0162; quotationCommand.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(quotationCommand_Click); //添加"采购单"菜单命令 purchaseOrderCommand = (Office.CommandBarButton)cmdBarControl.Controls.Add(Office.MsoControlType.msoControlButton, missing, missing, missing, true); purchaseOrderCommand.Caption = "采购单输入(&P)"; purchaseOrderCommand.Tag = "purchaseOrderCommand"; purchaseOrderCommand.FaceId = 0162; purchaseOrderCommand.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(purchaseOrderCommand_Click); } } catch (Exception e) { MessageBox.Show(e.Message); } }
//点击菜单事件 private void supplierCommand_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault) {
//点击“供应商”菜单后,会在excel 的sheet1的A1单元格写入"The menu command."; Globals.Sheet1.Range["A1", missing].Value2 = "The menu command ."; } void materialCommand_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault) { Globals.Sheet2.Activate(); //点击“物品表”会激活sheet2; } void questBuyBillCommand_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault) { Globals.Sheet1.Range["A3", missing].Value2 = "The menu command was clicked."; } void quotationCommand_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault) { Globals.Sheet1.Range["A2", missing].Value2 = "The menu command was clicked."; } void purchaseOrderCommand_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault) { throw new Exception("The method or operation is not implemented."); }
private void ThisWorkbook_Startup(object sender, System.EventArgs e) { //当工作簿启动时初始化菜单 CheckIfMenuBarExists(); AddMenuBar(); } private void ThisWorkbook_Shutdown(object sender, System.EventArgs e) { } #region VSTO Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InternalStartup() { this.Startup += new System.EventHandler(ThisWorkbook_Startup); this.Shutdown += new System.EventHandler(ThisWorkbook_Shutdown); } #endregion }
个人水平有限,具体语句的功能可以参考MSDN;而且我还有一个问题就是怎么将菜单分级和分组,例如下面的Ofice菜单:
希望那位高人能指点一下,谢谢。 |