title: How to detect whether an Excel workbook is closed (using Interop in C#)?
body: <p>I'm working on a C# project that uses <code>Microsoft.Office.Interop.Excel.Application</code> to read values from an Excel file:</p>
  
  <pre><code>try {
      Application xlApp = new Application();
      Workbook workbook = xlApp.Workbooks.Open(filename)
      // ... Load Excel values ...
  } finally {
      // ... Tidy up ...
  }
  </code></pre>
  
  <p>In the <code>finally</code> block I'd like to make sure everything is closed and disposed of properly so nothing hangs around in memory and Excel closes cleanly. Have seen various threads about what this code should look like (more complex than I thought!) but one thing it might include is:</p>
  
  <pre><code>if (workbook != null) {
      workbook.Close();
      // ... possibly also Marshal.FinalReleaseComObject(workbook);
  }
  </code></pre>
  
  <p>However, this throws an error if the workbook is already closed so how can I safely check this? Would prefer not to just catch the error if possible as this type of thing tends to distort debugging. Is there a clean way of finding out the workbook state before closing?</p>
  
  <p>One more q - am wondering if <code>workbook.Close()</code> is needed if <code>xlApp.Quit();</code> is done afterwards - would quitting the Excel application cause <code>workbook.Close()</code> (and any COM object releasing) to happen implicitly?</p>