在Excel中建立自定义菜单项 - 中国WEB开发者网络 (http://www.webasp.net) -- 技术教程 (http://www.webasp.net/article/) --- 在Excel中建立自定义菜单项 (http://www.webasp.net/article/14/13065.htm) |
| -- 作者:未知 -- 发布日期: 2004-09-14 |
| 演练:在Excel中建立自定义菜单项
Brian A. Randell MCW Technologies, LLC September 2003 Applies to: Microsoft® Visual Studio® Tools for the Microsoft Office System Microsoft Office Excel 2003 Microsoft Visual Studio .NET 2003 概述:Office CommandBar对象提供了增加菜单项和工具条按钮代码的途径。在这篇演练中,你将建立自定义菜单项目下的菜单条,并且增加代码来响应Office菜单。 内容: 介绍 建立菜单和工具条项目是微软Office的一个核心特点,虽然这次试验是示范在微软Office Excel 2003中使用这些项目,但是这些操作在Office Word中是类似的。(不同之处在于在Word中菜单名是Menu Bar,而在Excel中叫Worksheet Menu Bar。)你将在Excel主菜单中建立菜单项。然后,你增加此菜单项。最后,你增加Click事件代码来执行自定义代码。 提示:Office菜单和工具条的对象模块定义在Office.dll中,当你给微软Office System项目建立了一个新的Visual Studio Tools时,微软Visual Studio® .NET自动包含到此模块的引用。 先决条件 要完成此演练,下列软件和组件必须安装: • Microsoft Visual Studio .NET 2003 or Microsoft Visual Basic® .NET Standard 2003 • Microsoft Visual Studio Tools for the Microsoft Office System • Microsoft Office Professional Edition 2003 提示:假如你是Visual Basic .Net编程者,你需要设置Option Strict为On(或者你在每一个模块中增加Option Strict声明)。虽然这不是必须的,但是这可以保证你不会执行不安全的类型转换。在以后的时间里,利用此选项的好处将远远大于增加代码的困难。 开始 你将通过建立一个新的Visual Studio .NET的Excel项目来开始。 建立项目 使用微软Office System的Visual Studio Tool建立一个新的Excel工作簿项目(在Visual Basic .NET或是C#中)。 建立一个Excel工作簿项目 1. 开始Visual Studio .NET,在文件菜单上,指向新建,点击项目。 2. 在项目类型面板上,扩展微软Office System项目,接着选择Visual Basic 项目或Visual c#项目。 3. 在模板面板中选择Excel工作簿。 4. 起名为ExcelCommandBars,接着存储在当地硬盘。 5. 在微软Office项目向导中接受缺省值,点击完成。 Visual Studio .NET为你在代码编辑器中打开ThisWorkbook.vb或是ThisWorkboo.cs文件。 建立菜单栏项目 在Excel主菜单条上建立一个菜单栏项目需要你使用Add方法增加一个CommandBarControl。 在Excel中建立菜单栏项目 1. 在已存变量ThisApplication和ThisWorkbook下面增加下列变量: ' Visual Basic Private MainMenuBar As Office.CommandBar Private MenuBarItem As Office.CommandBarControl Private WithEvents MenuItem As Office.CommandBarButton // C# private Office.CommandBar MainMenuBar = null; private Office.CommandBarControl MenuBarItem = null; private Office.CommandBarButton MenuItem = null; 2. 在OfficeCodeBehing类中增加下列程序(通过项目模板建立),这段程序初始化了先前声明的MainMenuBar和MenuItemBar对象。 ' Visual Basic Private Sub InitMenuBarItems(ByVal Caption As String) Try MainMenuBar = ThisApplication.CommandBars( _ "Worksheet Menu Bar") MenuBarItem = MainMenuBar.Controls.Add( _ Office.MsoControlType.msoControlPopup, Temporary:=True) MenuBarItem.Caption = Caption Catch ex As Exception MessageBox.Show(ex.Message, _ ex.Source, MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub // C# private void InitMenuBarItems(string Caption) { try { MainMenuBar = ThisApplication.CommandBars["Worksheet Menu Bar"]; MenuBarItem = MainMenuBar.Controls.Add( Office.MsoControlType.msoControlPopup, Type.Missing, Type.Missing, Type.Missing, true); MenuBarItem.Caption = Caption; } catch (Exception ex) { MessageBox.Show(ex.Message, ex.Source, MessageBoxButtons.OK, MessageBoxIcon.Error); } } 3. 增加下列代码到已存在的ThisWorkbook_Open程序,这段代码调用你刚才建立的InitMenuBarItems程序。 ' Visual Basic InitMenuBarItems("&Custom Code") // C# InitMenuBarItems("&Custom Code"); 4. 选择文件菜单上的保存所有文件来保存整个方案。 5. 按F5运行项目,装入Excel和你的工作簿。 6. 在Excel中,查看菜单栏项目标签写着Custom Code的菜单显示在帮助菜单右边。如图一所示: 图一:有着自定义菜单栏项目的Excel 建立菜单项目 有了合适的自定义菜单栏,你就可以加入新的菜单中了。菜单项目表示为CommandBarControl对象,你将使用先前建立的菜单栏项目Controls集合的Add方法来建立一个新的CommandBarControl实例。 建立菜单项目 1. 增加下列程序到OfficeCodeBehind类中,这段程序建立了CommandBarControl并且设置其标题: ' Visual Basic Private Function CreateButton( _ ByVal Parent As Office.CommandBarPopup, _ ByVal Caption As String) As Office.CommandBarButton Try Dim cbc As Office.CommandBarControl cbc = Parent.Controls.Add( _ Office.MsoControlType.msoControlButton, Temporary:=True) cbc.Caption = Caption cbc.Visible = True Return DirectCast(cbc, Office.CommandBarButton) Catch ex As Exception MessageBox.Show(ex.Message, _ ex.Source, MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Function // C# private Office.CommandBarButton CreateButton( Office.CommandBarPopup Parent, string Caption) { Office.CommandBarControl cbc = null; try { cbc = Parent.Controls.Add( Office.MsoControlType.msoControlButton, Type.Missing, Type.Missing, Type.Missing, true); cbc.Caption = Caption; cbc.Visible = true; } catch (Exception ex) { MessageBox.Show(ex.Message, ex.Source, MessageBoxButtons.OK, MessageBoxIcon.Error); } return (Office.CommandBarButton) cbc; } 2. 增加下列代码到ThisWorkbook_Open程序中,下列代码调用了InitMenuBarItems程序: ' Visual Basic MenuItem = CreateButton( _ DirectCast(MenuBarItem, Office.CommandBarPopup), _ "Run Demo Code") // C# MenuItem = CreateButton( (Office.CommandBarPopup)MenuBarItem, "Run Demo Code"); 3. 选择文件菜单上的保存所有文件来保存整个方案。 4. 按F5运行此项目,装入Excel和你的工作簿。 5. 在Excel中点击自定义的顶级菜单,查看Run Demo Code菜单项。如下图二所示: 图二:增加了一个菜单项之后 拦截菜单项的点击事件 为了完成本次演练,你需要增加一个事件来处理自定义菜单项被点击之后的响应。 (只用在Visual Basic) 拦截菜单项点击事件 在Visual Basic .NET中完成下列步骤增加菜单项被点击的事件处理程序。 为自定义菜单项增加事件处理(Visual Basic) 1. 在代码编辑器的左上角的Class Name下拉列表中选择MenuItem。 2. 在代码编辑器的右上角的Method Name下拉列表中选择Click。 3. 修改MenuItem_Click程序,增加下列代码: ' Visual Basic MessageBox.Show(String.Format( _ "You just clicked the button labeled '{0}'.{1}" & _ "The name of your workbook is '{2}'.", _ Ctrl.Caption, Environment.NewLine, ThisWorkbook.Name), _ "MenuItem_Click", MessageBoxButtons.OK, _ MessageBoxIcon.Information) 4. 选择文件菜单中的保存所有文件来保存整个解决方案。 5. 按F5运行这个项目,装入Excel和你的工作簿。 6. 在Excel中,点击Custom Code菜单,接着选择Run Demo Code。 一个警告框出现,显示当前工作簿。 (C#)拦截点击菜单项目事件 在Visual C#中完成下列步骤来增加点击自定义菜单栏项目的事件处理。 为自定义菜单项目增加事件处理(C#) 1. 增加下列程序到OfficeCodeBehind类中: // C# private void MenuItem_Click( Office.CommandBarButton Ctrl, ref Boolean CancelDefault) { MessageBox.Show(String.Format( "You just clicked the button labeled '{0}'.\n" + "The name of your workbook is '{1}'.", Ctrl.Caption, ThisWorkbook.Name), "MenuItem_Click", MessageBoxButtons.OK, MessageBoxIcon.Information); } 2. 修改ThisWorkbook_Open程序,增加下列代码: // C# MenuItem.Click += new Microsoft.Office.Core. _ CommandBarButtonEvents_ClickEventHandler(MenuItem_Click); 提示:如果你输入完代码的第一行(直到+=),Visual Studio .NET将提示你按TAB键。代码编辑器将为你插入此行的剩余代码。这个自动完成的新特点使你更容易的完成事件处理程序。 测试本应用 现在你可以测试自己建立的自定义菜单项了。 测试本应用 1. 选择文件菜单的保存所有文件来保存整个项目。 2. 按F5运行这个项目,装入Excel和你的工作簿。 3. 在警告框中出现对你当前工作簿的描述。 结论 微软Office应用的一个核心特点是建立菜单和工具栏的能力。Office CommandBar对象提供了一种方法来自定义菜单和工具栏。虽然本次演练示范了你怎样增加代码来响应Excel菜单的点击,你将会发现在Word中的行为与此类似。 |
| webasp.net |