title: Running an Excel Macro via C#: Run a macro from one workbook on another?
body: <p>I am looking to run a macro, let's call it Macro01 from WorkSheet01 on WorkSheet02.</p>
  
  <p>Using <em>Microsoft.Office.Interop.Excel Namespace</em> I have opened a WorkSheet01.</p>
  
  <pre><code>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);
      }
  </code></pre>
  
  <p>I then use an automated script to pull a report. This report is opened via IE's download prompt and not the Interop.</p>
  
  <p>The problem comes when I try to run the macro via C# (I made another new Excel.ApplicationClass(); only so it compiled, I believe this is one of my missteps.)</p>
  
  <pre><code>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);
      }
  </code></pre>
  
  <p>When this method runs it runs the macro from Worksheet01 on Worksheet01 instead of Worksheet02. Also it was looking for the worksheet in My Documents so I moved it over to see what would happen.</p>
  
  <p><strong>Recap:</strong></p>
  
  <ol>
  <li>Open Worksheet01 </li>
  <li>Via scripting get and open a report (Worksheet02) from MSIE</li>
  <li>Run Macro01 from Worksheet01 on Worksheet02</li>
  </ol>
  
  <p><strong>Resources:</strong></p>
  
  <p><a href="http://support.microsoft.com/kb/306683">http://support.microsoft.com/kb/306683</a></p>
  
  <p><a href="http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.aspx">http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.aspx</a></p>
  
  <p>For those who would like to try it add this to your using directives:</p>
  
  <pre><code>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"
  </code></pre>