代码改变世界

Visual Studio中ExcelWorkBook项目创建加载项菜单

2013-01-16 12:43  BruceWayne  阅读(419)  评论(0编辑  收藏  举报
 1   public partial class Sheet1
 2     {
 3         private void Sheet1_Startup(object sender, System.EventArgs e)
 4         {
 5             //#region VSTO generated code
 6 
 7             //this.Application = (Excel.Application)Microsoft.Office.Tools.Excel.ExcelLocale1033Proxy.Wrap(typeof(Excel.Application), this.Application);
 8 
 9             //#endregion
10             this.AddMenu();
11      
12             
13         }
14 
15         private void Sheet1_Shutdown(object sender, System.EventArgs e)
16         {
17 
18         }
19 
20         #region VSTO 设计器生成的代码
21 
22         /// <summary>
23         /// 设计器支持所需的方法 - 不要
24         /// 使用代码编辑器修改此方法的内容。
25         /// </summary>
26         private void InternalStartup()
27         {
28             this.Startup += new System.EventHandler(Sheet1_Startup);
29             this.Shutdown += new System.EventHandler(Sheet1_Shutdown);
30         }
31 
32         #endregion
33 
34         private void AddMenu()
35         {
36             //获取Outlook的MenuBar 
37             Office.CommandBar bar = this.Application.CommandBars.ActiveMenuBar;
38             //创建顶级菜单 
39             Office.CommandBarControl menuTop = bar.Controls.Add(Office.MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing, bar.Controls.Count, true);
40             menuTop.Caption = "我的菜单";
41 
42             Office.CommandBarPopup commandBarPopupTmp = menuTop.Control as Office.CommandBarPopup;
43 
44             Office.CommandBarControl menuGroup = null;
45             Office.CommandBarControl menuCreateMail = null;
46 
47             if (commandBarPopupTmp.Controls.Count == 0)
48             {
49                 menuGroup = commandBarPopupTmp.Controls.Add(Office.MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing,System.Type.Missing, true);
50             }
51             else
52             {
53                 menuGroup = commandBarPopupTmp.Controls.Add(Office.MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing, commandBarPopupTmp.Controls.Count, true);
54             }
55 
56             menuCreateMail = commandBarPopupTmp.Controls.Add(Office.MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, 1, true);
57 
58             menuGroup.Caption = "菜单组";
59 
60             menuCreateMail.Caption = "保存焊接记录";
61 
62             Office.CommandBarButton buttonMenu = menuCreateMail as Office.CommandBarButton;
63             buttonMenu.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(buttonMenu_Click);
64           
65         }
66 
67 
68         void buttonMenu_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)
69         {
70             MessageBox.Show("222");
71         }
72     }