通过C#运行Excel宏:从另一个工作簿运行宏?

时间:2013-01-09 23:54:37

标签: c# excel vba office-automation

我希望运行一个宏,让我们从WorkSheet02上的WorkSheet01调用它。

使用 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#运行宏时出现了问题(我创建了另一个新的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运行宏。此外,它正在寻找我的文档中的工作表,所以我把它移过来看看会发生什么。

回顾:

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

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

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

    对于那些想要尝试的人,请将其添加到您的using指令中:

    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"
    

2 个答案:

答案 0 :(得分:8)

我找到了一个我想分享的解决方案。 首先,我删除了我打开Worksheet01的位。然后,我有自动脚本将.CSV保存到我的文档。然后我使用我必须打开Worksheet01的代码来打开下载的文件。此时的关键是Worksheet01位于Documents文件夹的Documents文件夹中。最后,我使用代码从Worksheet01运行宏,它在Worksheet02上运行。

    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);
    }

答案 1 :(得分:0)

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

this.Application.Run("mymacro");

编辑:

宏是Workbook,也许您需要在运行宏之前使Sheet2成为活动工作表,例如:

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