HOWTO:在 Visual C# .NET 中使用自动化创建 Excel 宏
2006-09-27 15:50
916 查看
HOWTO:在 Visual C# .NET 中使用自动化创建 Excel 宏
察看本文应用于的产品function loadTOCNode(){}
文章编号 | : | 303872 |
最后修改 | : | 2004年2月13日 |
修订 | : | 5.0 |
本页
概要
更多信息
创建 Visual C# .NET 应用程序示例的步骤
Additional Notes for Office XP
参考
var sectionFilter = "type != 'notice' && type != 'securedata' && type != 'querywords'";
var tocArrow = "/library/images/support/kbgraphics/public/en-us/downarrow.gif";
var depthLimit = 10;
var depth3Limit = 10;
var depth4Limit = 5;
var depth5Limit = 3;
var tocEntryMinimum = 1;
概要
loadTOCNode(1, 'summary');本文逐步介绍如何在 Microsoft Visual C# .NET 中使 Microsoft Excel 自动运行以创建包含新宏(该宏与 CommandBar 按钮关联)的工作簿。
回到顶端 |
更多信息
loadTOCNode(1, 'moreinformation');创建 Visual C# .NET 应用程序示例的步骤
loadTOCNode(2, 'moreinformation');1. | 启动 Microsoft Visual Studio .NET。 | ||||||
2. | 在文件菜单上,单击新建,然后单击项目。从 Visual C# 项目类型中选择 Windows 应用程序。默认情况下会创建 Form1。 | ||||||
3. | 添加对 Microsoft Excel 对象库和 Microsoft Visual Basic for Applications 扩展库的引用。为此,请按照下列步骤操作:
| ||||||
4. | 在视图菜单上,选择工具箱以显示工具箱,然后向 Form1 添加一个按钮。 | ||||||
5. | 双击 Button1。代码窗口打开并显示 Button1 的 Click 事件。将以下代码行添加到代码窗口顶部的 using 语句中:[code]using Office = Microsoft.Office.Core; using VBIDE = Microsoft.Vbe.Interop; using Excel = Microsoft.Office.Interop.Excel; | ||||||
6. | 在代码窗口中,将以下代码[code]private void button1_Click(object sender, System.EventArgs e) { }替换为: private void button1_Click(object sender, System.EventArgs e) { Excel.Application oExcel; Excel.Workbook oBook; VBIDE.VBComponent oModule; Office.CommandBar oCommandBar; Office.CommandBarButton oCommandBarButton; String sCode; Object oMissing = System.Reflection.Missing.Value; // Create an instance of Excel. oExcel = new Excel.Application(); // Add a workbook. oBook = oExcel.Workbooks.Add(oMissing); // Create a new VBA code module. oModule = oBook.VBProject.VBComponents.Add(VBIDE.vbext_ComponentType.vbext_ct_StdModule); sCode = "sub VBAMacro()/r/n" + " msgbox /"VBA Macro called/"/r/n" + "end sub"; // Add the VBA macro to the new code module. oModule.CodeModule.AddFromString(sCode); try { // Create a new toolbar and show it to the user. oCommandBar = oExcel.CommandBars.Add("VBAMacroCommandBar",oMissing, oMissing,/); oCommandBar.Visible = true; // Create a new button on the toolbar. oCommandBarButton = (Office.CommandBarButton) oCommandBar.Controls.Add( Office.MsoControlType.msoControlButton, oMissing, oMissing, oMissing, oMissing); // Assign a macro to the button. oCommandBarButton.OnAction = "VBAMacro"; // Set the caption of the button. oCommandBarButton.Caption = "Call VBAMacro"; // Set the icon on the button to a picture. oCommandBarButton.FaceId = 2151; } catch(Exception eCBError) { MessageBox.Show("VBAMacroCommandBar already exists.","Error"); } // Make Excel visible to the user. oExcel.Visible = true; // Set the UserControl property so Excel won't shut down. oExcel.UserControl = true; // Release the variables. oCommandBarButton = null; oCommandBar = null; oModule = null; oBook = null; oExcel = null; // Collect garbage. GC.Collect(); } | ||||||
7. | 按 F5 键生成并运行该程序。 | ||||||
8. | 单击 Button1 启动 Microsoft Excel,插入 Visual Basic for Applications (VBA) 代码,然后添加一个新的 CommandBar。单击 CommandBar 上的按钮以运行 VBA 宏。 |
回到顶端 |
Additional Notes for Office XP
loadTOCNode(2, 'moreinformation');Microsoft Office 2003 和 Microsoft Office XP 应用程序具有一个安全选项,以允许对 VBA 对象模型进行编程访问。如果此设置为关(默认设置),可能会在运行该代码示例时出现一个错误。 有关此设置和如何纠正该错误的其他信息,请单击下面的文章编号,以查看 Microsoft 知识库中相应的文章:
282830 (http://support.microsoft.com/kb/282830/) PRB:Programmatic Access to Office XP VBA Project Is Denied
回到顶端 |
参考
loadTOCNode(1, 'references');有关其他信息,请单击下面的文章编号,以查看 Microsoft 知识库中相应的文章:
303871 (http://support.microsoft.com/kb/303871/) Create an Excel Macro Programmatically from Visual Basic .NET
194611 (http://support.microsoft.com/kb/194611/) Create and Call an Excel Macro Programmatically from VB
回到顶端 |
• | Microsoft Visual C# .NET 2003 标准版 |
• | Microsoft Visual C# .NET 2002 标准版 |
• | Microsoft Office Excel 2003 |
• | Microsoft Excel 2002 标准版 |
回到顶端 |
关键字: | kbhowto kbpia KB303872 |
回到顶端 |
相关文章推荐
- HOWTO:在 Visual C# .NET 中使用自动化创建 Excel 宏
- 如何使用 Visual C# 2005 或 Visual C# .NET 向 Excel 工作簿传输数据
- 使用 Visual C# .NET 在 ASP.NET 中创建自定义错误报告
- 如何使用 ASP.NET、ADO.NET 和 Visual C# .NET 查询和显示 Excel 数据
- 如何使用 Visual C# .NET 处理 Excel 事件
- 使用 Visual C# .NET 向 Excel 工作簿传输数据
- 使用 Visual C# .NET 向 Excel 工作簿传输数据
- 如何使用 ASP.NET、ADO.NET 和 Visual C# .NET 查询和显示 Excel 数据
- 使用 Visual C# .NET 向 Excel 工作簿传输数据
- 使用 Visual C# .NET 在 ASP.NET 中创建自定义错误报告
- 如何使用 Visual C# 2005 或 Visual C# .NET 向 Excel 工作簿传输数据
- [转载]如何使用 Visual C# 2005 或 Visual C# .NET 向 Excel 工作簿传输数据
- 如何使用 Visual C# .NET 创建 ASP.NET HTTP 模块
- 使用 Visual C# .NET 向 Excel 工作簿传输数据 (转发)
- 如何使用 Visual C# 2005 或 Visual C# .NET 向 Excel 工作簿传输数据
- 使用 Visual C# .NET 在 ASP.NET 中创建自定义错误报告
- HOWTO:使用 WebBrowser 控件在 Visual C# .NET 中打开 Office 文档(转贴)
- 如何使用 Visual C# 2005 或 Visual C# .NET 向 Excel 工作簿传输数据
- 如何使用 ASP.NET、ADO.NET 和 Visual C# .NET 查询和显示 Excel 数据
- 使用 Visual C# .NET 在 ASP.NET 中创建自定义错误报告