title: Error when calling Excel macro from C#
body: <p>I am trying to call a macro from an Excel file using <code>C# 4.5</code>. My Excel version is 2010.</p>
  
  <p>When I try to call the macro, I get the following error:</p>
  
  <pre><code>Cannot run the macro 'MacroName'. The macro may not be available in this workbook or all macros may be disabled.
  </code></pre>
  
  <p>I have googled a lot for this, but nothing really seems to work.</p>
  
  <p>I have opened up macro security, as the following screen shot shows:</p>
  
  <p><img src="https://i.stack.imgur.com/VVRTO.png" alt="Macro Security"></p>
  
  <p>The code I am using is as follows:</p>
  
  <pre><code>Excel.Application book = null;
  Excel.Workbooks workbooks = null;
  Excel.Workbook macroWorkbook = null;
  Excel.Workbook destinationWorkbook = null;
  
  try
  {
      book = new Excel.Application();
  
      workbooks = book.Workbooks;
      macroWorkbook = workbooks.Open(@"D:\Work\Macros.xls");
      destinationWorkbook = workbooks.Open(@"D:\Work\Destination.xlsx");
  
      book.Run("MacroName");
  
      macroWorkbook.Close(false);
      destinationWorkbook.Close(true);
  }
  catch (Exception ex)
  {
      throw ex; // the finally will be executed before this is thrown
  }
  finally
  {
      book.Quit();
  
      System.Runtime.InteropServices.Marshal.ReleaseComObject(macroWorkbook);
      System.Runtime.InteropServices.Marshal.ReleaseComObject(destinationWorkbook);
      System.Runtime.InteropServices.Marshal.ReleaseComObject(workbooks);
      System.Runtime.InteropServices.Marshal.ReleaseComObject(book);
  
      macroWorkbook = null;
      destinationWorkbook = null;
      workbooks = null;
      book = null;
  }
  </code></pre>
  
  <p>The actual macro is stored in <code>Macros.xls</code> and will run on the <code>Destination.xslx</code>. When I run this macro in <code>Excel</code> itself, there is no problem.</p>