title: How to access an already opened Excel file in C#?
body: <p>I have an excel workbook opened via double-clicking it in windows explorer but cannot access it in code</p>
  
  <pre><code>Excel.Application xlApp = (Application)Marshal.GetActiveObject("Excel.Application");
  Excel.Workbooks xlBooks = xlApp.Workbooks;
  </code></pre>
  
  <p>xlBooks.Count equals 0, why isn't it referencing my opened workbook?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Here are the various scenarios and what is happening:</p>
  
  <p>Scenario 1: If the file is not already open</p>
  
  <ul>
  <li>Code opens workbook, I am happy.</li>
  </ul>
  
  <p>Scenario 2: If the file is initially opened from code and I close and reopen the app</p>
  
  <ul>
  <li>Code references file just fine <code>xlBooks.Count</code> equals 1, I am happy.</li>
  </ul>
  
  <p><em>Scenario 3</em>: If the file is initially opened not from code, and via double-clicking it in explorer</p>
  
  <ul>
  <li>Code opens another instance of the file <code>xlBooks.Count</code> equals 0, <strong>I am in a rage!</strong></li>
  </ul>
  
  <p>Here is the entire code as it stands right now</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.IO;
  using System.Linq;
  using System.Runtime.InteropServices;
  using Microsoft.Office.Interop.Excel;
  
  public class ExcelService : IExcelService
  {
      const string _filePath = @"C:\Somewhere";
      const string _fileName = @"TestFile.xlsb";
      string _fileNameAndPath = Path.Combine(_filePath, _fileName);
  
      Application xlApp;
      Workbooks xlBooks;
      Workbook xlBook;
      Worksheet xlSheet;
  
      public ExcelService()
      {
          try
          {
              xlApp = (Application)Marshal.GetActiveObject("Excel.Application");
              xlBooks = xlApp.Workbooks;
  
              var numBooks = xlBooks.Count;
              Log.Info("Number of workbooks: {0}".FormatWith(numBooks));
              if (numBooks &gt; 0)
              {
                  xlBook = xlBooks[1];
                  Log.Info("Using already opened workbook");
              }
              else
              {
                  xlBook = xlBooks.Open(_fileNameAndPath);
                  Log.Info("Opening workbook: {0}".FormatWith(_fileNameAndPath));
              }
  
              xlSheet = (Worksheet)xlBook.Worksheets[1];
  
              // test reading a named range
              string value = xlSheet.Range["TEST"].Value.ToString();
              Log.Info(@"TEST: {0}".FormatWith(value));
  
              xlApp.Visible = true;
          }
          catch (Exception e)
          {
              Log.Error(e.Message);
          }
      }
  
      ~ExcelService()
      {
          GC.Collect();
          GC.WaitForPendingFinalizers();
  
          try
          {
              Marshal.FinalReleaseComObject(xlSheet);
          }
          catch { }
  
          try
          {
              Marshal.FinalReleaseComObject(xlBook);
          }
          catch { }
  
          try
          {
              Marshal.FinalReleaseComObject(xlBooks);
          }
          catch { }
  
          try
          {
              Marshal.FinalReleaseComObject(xlApp);
          }
          catch { }
      }
  }
  </code></pre>