通过C#运行Excelmacros:在另一个工作簿上运行macros?

我正在运行一个macros,我们从WorkSheet02上的WorkSheet01调用Macro01。

使用Microsoft.Office.Interop.Excel命名空间我打开了一个WorkSheet01。

public void Main_CodedStep() { // Object for missing (or optional) arguments. object oMissing = System.Reflection.Missing.Value; // Create an instance of Microsoft Excel Excel.ApplicationClass oExcel = new Excel.ApplicationClass(); // Make it visible oExcel.Visible = true; // Open Worksheet01.xlsm Excel.Workbooks oBooks = oExcel.Workbooks; Excel._Workbook oBook = null; oBook = oBooks.Open("C:\\Users\\Admin\\Documents\\Worksheet01.xlsm", oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing); } 

然后,我使用自动化脚本来提取报告。 这个报告是通过IE的下载提示打开的,而不是Interop。

问题出现在我试图通过C#运行macros(我做了另一个新的Excel.ApplicationClass();只是这样编译,我相信这是我的失误之一。

 public void FirstMacro_CodedStep() { // Create an instance of Microsoft Excel Excel.ApplicationClass oExcel = new Excel.ApplicationClass(); Console.WriteLine("ApplicationClass: " + oExcel); // Run the macro, "First_Macro" RunMacro(oExcel, new Object[]{"Worksheet01.xlsm!First_Macro"}); //Garbage collection GC.Collect(); } private void RunMacro(object oApp, object[] oRunArgs) { oApp.GetType().InvokeMember("Run", System.Reflection.BindingFlags.Default | System.Reflection.BindingFlags.InvokeMethod, null, oApp, oRunArgs); } 

运行此方法时,Worksheet01上的Worksheet01而不是Worksheet02运行macros。 另外它正在寻找我的文档中的工作表,所以我把它移动到看看会发生什么。

回顾:

  1. 打开Worksheet01
  2. 通过脚本获取并打开来自MSIE的报告(Worksheet02)
  3. 在Worksheet02上从Worksheet01运行Macro01

资源:

http://support.microsoft.com/kb/306683

http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.aspx

对于那些想要尝试的人来说,把它添加到你的使用指令中:

 using System.Reflection; using Microsoft.Office.Core; //Added to Project Settings' References from C:\Program Files (x86)\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Office14 - "office" using Excel = Microsoft.Office.Interop.Excel; //Added to Project Settings' References from C:\Program Files (x86)\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Office14 - "Microsoft.Office.Interop.Excel" 

我find了一个我想分享的解决scheme。 首先,我删除了打开Worksheet01的位。 然后我有我的自动脚本将.CSV保存到我的文档。 然后我用我打开Worksheet01打开下载的文件的代码。 在这一点上的关键是Worksheet01是与Worksheet02在文档文件夹中。 最后,我使用代码来运行在Worksheet02上运行的Worksheet01的macros。

  public void WebTest_CodedStep() { // Object for missing (or optional) arguments. object oMissing = System.Reflection.Missing.Value; // Create an instance of Microsoft Excel Excel.ApplicationClass oExcel = new Excel.ApplicationClass(); // Make it visible oExcel.Visible = true; // Define Workbooks Excel.Workbooks oBooks = oExcel.Workbooks; Excel._Workbook oBook = null; // Get the file path string path = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); path = path + "\\Worksheet02.csv"; //Open the file, using the 'path' variable oBook = oBooks.Open(path, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing); // Run the macro, "First_Macro" RunMacro(oExcel, new Object[]{"Worksheet01.xlsm!First_Macro"}); // 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; //Garbage collection GC.Collect(); } private void RunMacro(object oApp, object[] oRunArgs) { oApp.GetType().InvokeMember("Run", System.Reflection.BindingFlags.Default | System.Reflection.BindingFlags.InvokeMethod, null, oApp, oRunArgs); } 

我运行这个C#VSTO代码来调用VBAmacros,这是我使用的语法:

 this.Application.Run("mymacro"); 

编辑:

macros是工作簿广泛的,也许你需要在运行macros之前使Sheet2成为活动工作表,例如:

 foreach (Worksheet worksheet in workbook.Sheets.ComLinq<Worksheet>()) { if (worksheet.Name == "Sheet2") worksheet.Activate(); }