Pagh
Pagh

Reputation:

System.Reflection.TargetInvocationException when running excel macro from C#

I’m trying to run a excel macro from my C# code, but I'm getting the following error when trying to execute the code.

The code is:

    static void Main(string[] args)
    {
            object oMissing = System.Reflection.Missing.Value;

            Excel.ApplicationClass oExcel = new Excel.ApplicationClass();
            oExcel.Visible = true;


            Excel.Workbooks oBooks = oExcel.Workbooks;
            Excel._Workbook oBook = null;
            oBook = oBooks.Open(@"c:\Afstemning_BEC_SCD_PROD.xls", oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);

            // Run the macros.'
            RunMacro(oExcel, new Object[] { "macro_name" });


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

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

And the complete error message:

Unhandled Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Runtime.InteropServices. COMException (0x800A9C68): Exception from HRESULT: 0x800A9C68 --- End of inner exception stack trace --- at System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters) at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Bi nder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers , CultureInfo culture, String[] namedParams) at System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder bind er, Object target, Object[] args) at scd_afstemning_vhs.Program.RunMacro(Object oApp, Object[] oRunArgs) in C:\ Documents and Settings\lfr\Desktop\sub\scd\scd_afstemning_vhs\scd_afstemning_vhs \Program.cs:line 54 at scd_afstemning_vhs.Program.Main(String[] args) in C:\Documents and Setting s\lfr\Desktop\sub\scd\scd_afstemning_vhs\scd_afstemning_vhs\Program.cs:line 38

Thanks.

Upvotes: 0

Views: 5324

Answers (3)

Alex Fainshtein
Alex Fainshtein

Reputation: 1153

This COMException (0x800A9C68) happens, in particular, if you use End statement in VBA code to terminate the macro. Use Exit Function or Exit Sub instead.

Quote from MSDN forum: End is a bit too abrupt and doesn't really clean things up properly

Upvotes: 1

harlam357
harlam357

Reputation: 1491

I suspect the problem here is with the Excel installation (see my comment on the question). The code running in our application invokes an Excel macro using an identical call as seen in the question. This method is also documented here.

My user's machine was running Office 2007 RTM. I tried performing a repair on Office. The repair completed but the error still remained when running the code. I then applied Office 2007 Service Pack 2 and the problem went away. I don't believe this behavior has anything specifically to do with a fix that is included in SP2 but simply the actions taken by the service pack installer fixed the broken piece of Excel as a side effect. Presumably, a simple uninstall and full reinstall of Office 2007 RTM would yield the same result.

Upvotes: 0

shahkalpesh
shahkalpesh

Reputation: 33474

Can't you do oApp.Run(myMacroNameInAStringVariable); instead?
Why are you using reflection?

Upvotes: 0

Related Questions