title: Running an Excel macro on a different workbook
body: <p>I am trying to run a VBA macro from one workbook on another workbook.<br>
  I am using the Microsoft.Office.Interop.Excel (or equivalent) object (add a reference (COM) to Microsoft Excel 12.0 Object Library if you want to use it) to edit and use Excel documents from a C# executable.</p>
  
  <p>For example, document one has some data in it. Document two contains macros to format document one. Document one is new every day, so I can't store the macros there. I'm trying to run a macro from document two on document one using the Excel.Application.Run().</p>
  
  <p>The example macro I'm using is simple (stored in the Microsoft Excel Object: ThisWorkbook):</p>
  
  <pre><code>Sub Test()
    Sheets("Sheet1").Select
    Range("A1").Value = 32
  End Sub
  </code></pre>
  
  <p>I need this to run on a different workbook. I can run it on the same workbook with the following code:</p>
  
  <pre><code>Application xlApp = new Application(); //Excel app
  Workbook xlWbk = null;
  try
  {
    xlWbk = xlApp.Workbooks.Open(DocumentTwoLocation);
    xlApp.Run("'" + DocumentTwoLocation + "'!" + MacroName); //MacroName example: ThisWorkbook.Test
  }
  finally
  {
    if (xlWbk != null)
      try
      {
        xlWbk.Close(true); //Saves changes
      }
      catch
      {
        xlWbk.Close(false);
      }
    xlApp.Quit();
  }
  </code></pre>
  
  <p>When I change DocumentTwoLocation in the xlApp.Workbooks.Open to DocumentOneLocation, the macro will not run. A COMException is thrown (HRESULT: 0x800A03EC) which is NameNotFound (only thrown when the xlApp.Run() is used). The file addresses are correct. Even if I open both documents beforehand like so:</p>
  
  <pre><code>Application xlApp = new Application(); //Excel app
  Workbook xlWbk = null;
  Workbook xlMacroBook = null;
  try
  {
    xlWbk = xlApp.Workbooks.Open(DocumentOneLocation);
    xlMacroBook = xlApp.Workbooks.Open(DocumentTwoLocation);
    xlApp.Run("'" + DocumentTwoLocation + "'!" + MacroName); //MacroName example: ThisWorkbook.Test
  }
  finally
  {
    if (xlWbk != null)
      try
      {
        xlWbk.Close(true); //Saves changes
      }
      catch
      {
        xlWbk.Close(false);
      }
    if (xlMacroBook != null)
      xlMacroBook.Close(false) //Don't save changes
    xlApp.Quit();
  }
  </code></pre>
  
  <p>An error still occurs (same 0x800A03EC exception).</p>
  
  <p>MSDN has virtually no documentation on what I am missing or how I should approach this (<a href="https://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.applicationclass.run.aspx" rel="nofollow noreferrer">https://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.applicationclass.run.aspx</a>)</p>
  
  <p>Something similar has been asked here:
  <a href="https://stackoverflow.com/questions/19508569/error-when-calling-excel-macro-from-c-sharp">Error when calling Excel macro from C#</a>
  and I've done all I can to match what they have written (including adding the ReleaseComObject, and setting to null after) but to no avail.</p>