如何使用C#运行VB宏脚本?

时间:2016-02-01 12:43:43

标签: c# excel winforms

我有一个在Excel文件中使用的VB脚本宏,它运行良好(手动)。我试图使用以下方法将其转换为小型APP:

private static void RunMacro()
{

    Excel.Application book = null;
    Excel.Workbooks workbooks = null;
    Excel.Workbook macroWorkbook = null;
    Excel.Workbook destinationWorkbook = null;

    try
    {
        book = new Excel.Application();

        workbooks = book.Workbooks;
        macroWorkbook = workbooks.Open(System.Windows.Forms.Application.StartupPath + "\\NewMacro.xltm");
        destinationWorkbook = workbooks.Open(System.Windows.Forms.Application.StartupPath + "\\TEST.csv");

        book.Run("NewMacro.xltm");

        macroWorkbook.Close(false);
        destinationWorkbook.Close(true);
    }
    catch (Exception ex)
    {
        throw ex; // the finally will be executed before this is thrown
    }
    finally
    {
        book.Quit();

        System.Runtime.InteropServices.Marshal.ReleaseComObject(macroWorkbook);
        System.Runtime.InteropServices.Marshal.ReleaseComObject(destinationWorkbook);
        System.Runtime.InteropServices.Marshal.ReleaseComObject(workbooks);
        System.Runtime.InteropServices.Marshal.ReleaseComObject(book);

        macroWorkbook = null;
        destinationWorkbook = null;
        workbooks = null;
        book = null;
    }
}

我收到以下错误:

  

其他信息:无法运行宏'NewMacro.xltm'。该   宏可能在此工作簿中不可用或所有宏都可用   禁用。

2 个答案:

答案 0 :(得分:1)

 book.Run("NewMacro.xltm");

这意味着您尝试打开启用宏的电子表格而不是宏本身。

http://filext.com/file-extension/XLTM

一个老问题......这样的事情会起作用

Running an Excel Macro via C#: Run a macro from one workbook on another?

答案 1 :(得分:0)

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);
}
相关问题