Errors when calling certain Excel VBA macros from C#

有些话、适合烂在心里 提交于 2019-12-06 02:08:16

There could be several things your missing. First here is the code I used to get it working, using the .Net reference Microsoft.Office.Interop.Excel v14 (for Office 2010):

using System;
using Microsoft.Office.Interop.Excel;

namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
    RunVBATest();
}

public static void RunVBATest()
{
    //System.Globalization.CultureInfo oldCI = System.Threading.Thread.CurrentThread.CurrentCulture;
    //System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
    //object oMissing = System.Reflection.Missing.Value;
    Application oExcel = new Application();
    oExcel.Visible = true;
    Workbooks oBooks = oExcel.Workbooks;
    _Workbook oBook = null;
    oBook = oBooks.Open("C:\\temp\\Book1.xlsm");

    // Run the macro.
    RunMacro(oExcel, new Object[] { "TestMsg" });

    // Quit Excel and clean up.
    oBook.Saved = true;
    oBook.Close(false);
    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;
    //System.Threading.Thread.CurrentThread.CurrentCulture = oldCI;
}

private static void RunMacro(object oApp, object[] oRunArgs)
{
    oApp.GetType().InvokeMember("Run",
        System.Reflection.BindingFlags.Default |
        System.Reflection.BindingFlags.InvokeMethod,
        null, oApp, oRunArgs);
}
}
}

Second make sure you put the Macro code in a Module (a Global BAS file)..

Public Sub TestMsg()

MsgBox ("Hello Stackoverflow")

End Sub

Third make sure you enable Macro Security and Trust access to the VBA Project object model:

Also does anyone know if for this case there is a way to debug Excel VBA from Visual Studio 2012, the Com Exceptions are pretty unhelpful... – Sunny Sampath

When you overcome the Com exception, stepping through the code will cross processes from VS into Excel's VBE and then back to VS.

Edit:

Here is the VBA code to clear contents:

Dim activeSheet As Worksheet
Set activeSheet = Excel.activeSheet
activeSheet.UsedRange.Clear

Or if you want to delete a NameRange called TestRange:

Dim range As range
Set range = activeSheet.range("TestRange")
range.Clear

The other TestMacro's are too specific to your work for me to understand. Good luck!

Well apparently there was a very easy solution. Instead of using RunMacro function...using the application method "Run" works for all macros. I used the line

oExcel.Run("TestMacro"); 

instead of calling

private static void RunMacro(object oApp, object[] oRunArgs)
{
    oApp.GetType().InvokeMember("Run",
        System.Reflection.BindingFlags.Default |
        System.Reflection.BindingFlags.InvokeMethod,
        null, oApp, oRunArgs);
}

with

RunMacro(oExcel, new Object[] { "TestMsg" });

Arghh..this issue stole 2 days of my life!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!