title: System.Reflection.TargetInvocationException when running excel macro from C#
body: <p>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. </p>
  
  <p>The code is: </p>
  
  <pre><code>    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);
      }
  </code></pre>
  
  <p>And the complete error message: </p>
  
  <p>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</p>
  
  <p>Thanks.</p>