Accessing an open Excel Workbook in C#

14,213

Solution 1

Instead of instantiating a new instance, check for an existing one:

try
{
  Microsoft.Office.Interop.Excel.Application app = 
      System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
}
catch
{
  // Excel is not running.
}

Solution 2

Try this code:

using Excel = Microsoft.Office.Interop.Excel;

public Excel.Application xlApp;
public Excel.Workbook xlWorkBook;
public Excel.Worksheet xlWorkSheet;

public void ExcelTransferData()
{
   xlApp = new Microsoft.Office.Interop.Excel.Application();
   xlApp = (Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");

   foreach (Excel.Workbook item in xlApp.Workbooks)
   {
        //Select the excel target 'NAME'
        if (item.Name == "Template.xlsx")
        {
            xlWorkBook = item; 
            break;
        }

        //Select the target workbook
        xlWorkSheet = xlWorkBook.Sheets[1] as Excel.Worksheet;
        //Set cell value
        xlWorkSheet.Cells[5, 1] = "davinceleecode";
   }
}
Share:
14,213

Related videos on Youtube

Jon Erickson
Author by

Jon Erickson

Software Engineer with experience developing and supporting systems across the entire stack with recent focus on back-end, scalable systems. Also, recently completed an MS in Computer Science from Georgia Tech.

Updated on June 04, 2022

Comments

  • Jon Erickson
    Jon Erickson about 2 years

    I need to access an excel file that is already open. I thought just inspecting the .Workbooks property that it would be there but it isn't. What is the right way to get a reference to the open workbook?

    var app = new Microsoft.Office.Interop.Excel.Application();
    
    // the count is 0 =(
    app.Workbooks.Count == 0;
    

    EDIT

    I can get a reference to the Excel Application via...

    app = (Excel.Application)Marshal.GetActiveObject("Excel.Application");
    

    but app.Workbooks.Count is still 0 why isn't it able to get a reference to the opened workbook?

  • Jon Erickson
    Jon Erickson almost 13 years
    this seems to get a reference to the Excel application app != null but app.Workbooks.Count is still 0. Why isn't it getting a reference to the opened workbook?
  • Matthew maz
    Matthew maz almost 13 years
    Excel interop can be annoying. The most important thing is to ensure that you always release the Excel application object. Otherwise, you will have ghost instances of EXCEL.exe running. So, close all Excel instances, then try the code again. Finally, read this article on how to properly release Excel support.microsoft.com/default.aspx?scid=kb;EN-US;317109
  • Bryan Crosby
    Bryan Crosby almost 13 years
    Also, see this thread for proper cleanup and discussion: stackoverflow.com/questions/158706/…
  • Jon Erickson
    Jon Erickson almost 13 years
    I don't think my issue is in regards to release the application, although that link helped... but I can't even access the opened workbook.. I can get to the Excel Application itself, but I can't find the workbook
  • Matthew maz
    Matthew maz almost 13 years
    To verify - Did you check how many instances of Excel are opened in task manager?
  • ManInMoon
    ManInMoon over 12 years
    Jon, I am trying to do the same. Did you ever manage to link to an already open worksheet?
  • sigil
    sigil over 11 years
    I'm also trying to do this, did you ever find a solution?
  • noztol
    noztol almost 8 years
    Looks like as of 2012 no solution for the Workbooks.Count equal to 0?
  • Automate This
    Automate This about 7 years
    This answer seems to have nothing to do with the question.
  • Chris R.
    Chris R. about 7 years
    Still looking for a solution for the he Workbooks.Count equal to 0
  • Beginner
    Beginner over 4 years
    If the opened Excel is in PROTECTED VIEW you may find this answer helpful stackoverflow.com/questions/58458188/…

Related