Visual C# .NET 中使用自动化运行 Office 宏

    技术2022-05-11  18

    创建 Visual C# .NET 自动化客户端 启动 Microsoft Visual Studio .NET。在“文件”菜单上,单击“新建”,然后单击“项目”。在“项目类型”下,单击“Visual C# 项目”,然后单击“模板”下的“Windows 应用程序”。默认情况下会创建 Form1。 添加对 Access、Excel、PowerPoint 和 Word 对象库的引用。为此,请按照下列步骤操作: 在“项目”菜单上,单击“添加引用”。 在“COM”选项卡上,找到“Microsoft Word 10.0 对象库或 Microsoft Word 11.0 对象库”,然后单击“选择”。注意:如果您使用的是 Office XP 且尚未执行此操作,Microsoft 建议您下载并安装 Microsoft Office XP 主互操作程序集 (PIA)。有关 Office XP PIA 的其他信息,请单击下面的文章编号,以查看 Microsoft 知识库中相应的文章: 328912 (http://support.microsoft.com/kb/328912/ ) Microsoft Office XP 主互操作程序集 (PIA) 可供下载 为 Access、Excel 和 PowerPoint 对象库重复上一步。 在“添加引用”对话框中,单击“确定”以接受您的选择。如果系统提示您为选定的库生成包装,请单击“是”。 在“视图”菜单上,单击“工具箱”。向 Form1 添加一个“Combo Box”控件和一个“Button”控件。 双击“Button1”为按钮的“Click”事件处理程序生成定义。 将下面的代码粘贴到“button1_Click”处理程序中: private void button1_Click(object sender, System.EventArgs e) { // Object for missing (or optional) arguments. object oMissing = System.Reflection.Missing.Value; //Switch based on the user selection. switch (comboBox1.SelectedIndex) { case 0: // Create an instance of Microsoft Access, make it visible, // and open Db1.mdb. Access.ApplicationClass oAccess = new Access.ApplicationClass(); oAccess.Visible = true; oAccess.OpenCurrentDatabase("c://db1.mdb", false, ""); // Run the macros. RunMacro(oAccess, new Object[]{"DoKbTest"}); RunMacro(oAccess, new Object[]{"DoKbTestWithParameter", "Hello from C# Client."}); // Quit Access and clean up. oAccess.DoCmd.Quit(Access.AcQuitOption.acQuitSaveNone); System.Runtime.InteropServices.Marshal.ReleaseComObject (oAccess); oAccess = null; break; case 1: // Create an instance of Microsoft Excel, make it visible, // and open Book1.xls. Excel.ApplicationClass oExcel = new Excel.ApplicationClass(); oExcel.Visible = true; Excel.Workbooks oBooks = oExcel.Workbooks; Excel._Workbook oBook = null; oBook = oBooks.Open("c://book1.xls", oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing); // Run the macros. RunMacro(oExcel, new Object[]{"DoKbTest"}); RunMacro(oExcel, new Object[]{"DoKbTestWithParameter", "Hello from C# Client."}); // Quit Excel and clean up. oBook.Close(false, oMissing, oMissing); System.Runtime.InteropServices.Marshal.ReleaseComObject (oBook); oBook = null; System.Runtime.InteropServices.Marshal.ReleaseComObject (oBooks); oBooks = null; oExcel.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject (oExcel); oExcel = null; break; case 2: // Create an instance of PowerPoint, make it visible, // and open Pres1.ppt. PowerPoint.ApplicationClass oPP = new PowerPoint.ApplicationClass(); oPP.Visible = MsoTriState.msoTrue; PowerPoint.Presentations oPresSet = oPP.Presentations; PowerPoint._Presentation oPres = oPresSet.Open("c://pres1.ppt", MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoTrue); // Run the macros. RunMacro(oPP, new Object[]{"'pres1.ppt'!DoKbTest"}); RunMacro(oPP, new Object[]{"'pres1.ppt'!DoKbTestWithParameter", "Hello from C# Client."}); // Quit PowerPoint and clean up. oPres.Close(); System.Runtime.InteropServices.Marshal.ReleaseComObject (oPres); oPres = null; System.Runtime.InteropServices.Marshal.ReleaseComObject (oPresSet); oPresSet = null; oPP.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject (oPP); oPP = null; break; case 3: // Create an instance of Word, make it visible, // and open Doc1.doc. Word.ApplicationClass oWord = new Word.ApplicationClass(); oWord.Visible = true; Word.Documents oDocs = oWord.Documents; object oFile = "c://doc1.doc"; // If the Microsoft Word 10.0 Object Library is referenced // use the following code. Word._Document oDoc = oDocs.Open(ref oFile, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); // If the Microsoft Word 11.0 Object Library is referenced comment // the previous line of code and uncomment the following code. //Word._Document oDoc = oDocs.Open(ref oFile, ref oMissing, //ref oMissing, ref oMissing, ref oMissing, ref oMissing, //ref oMissing, ref oMissing, ref oMissing, ref oMissing, //ref oMissing, ref oMissing, ref oMissing, ref oMissing, //ref oMissing, ref oMissing); // Run the macros. RunMacro(oWord, new Object[]{"DoKbTest"}); RunMacro(oWord, new Object[]{"DoKbTestWithParameter", "Hello from C# Client."}); // Quit Word and clean up. oDoc.Close(ref oMissing, ref oMissing, ref oMissing); System.Runtime.InteropServices.Marshal.ReleaseComObject (oDoc); oDoc = null; System.Runtime.InteropServices.Marshal.ReleaseComObject (oDocs); oDocs = null; oWord.Quit(ref oMissing, ref oMissing, ref oMissing); System.Runtime.InteropServices.Marshal.ReleaseComObject (oWord); oWord = null; break; } GC.Collect(); //Garbage collection. } 在“button1_Click”处理程序之后添加下面的函数: private void RunMacro(object oApp, object[] oRunArgs) { oApp.GetType().InvokeMember("Run", System.Reflection.BindingFlags.Default | System.Reflection.BindingFlags.InvokeMethod, null, oApp, oRunArgs); } 在“视图”菜单上,单击“设计器”,然后双击“Form1”,以生成该窗体的“Load”事件的定义。 将下面的代码粘贴到“Form1_Load”处理程序中: private void Form1_Load(object sender, System.EventArgs e) { comboBox1.DropDownStyle = ComboBoxStyle.DropDownList; comboBox1.Items.AddRange(new object[] {"Access", "Excel", "PowerPoint", "Word"}); comboBox1.SelectedIndex = 0; } 滚动到代码窗口的顶部,然后将下面的代码行添加到“using”指令列表的末尾: using System.Reflection; using Access = Microsoft.Office.Interop.Access; using Excel = Microsoft.Office.Interop.Excel; using PowerPoint = Microsoft.Office.Interop.PowerPoint; using Word = Microsoft.Office.Interop.Word; using Microsoft.Office.Core;

     

    http://support.microsoft.com/kb/306683/zh-cn


    最新回复(0)