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";
}
}
Related videos on Youtube
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, 2022Comments
-
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 still0
why isn't it able to get a reference to the opened workbook? -
Jon Erickson almost 13 yearsthis seems to get a reference to the Excel application
app != null
butapp.Workbooks.Count
is still0
. Why isn't it getting a reference to the opened workbook? -
Matthew maz almost 13 yearsExcel 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 almost 13 yearsAlso, see this thread for proper cleanup and discussion: stackoverflow.com/questions/158706/…
-
Jon Erickson almost 13 yearsI 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 almost 13 yearsTo verify - Did you check how many instances of Excel are opened in task manager?
-
ManInMoon over 12 yearsJon, I am trying to do the same. Did you ever manage to link to an already open worksheet?
-
sigil over 11 yearsI'm also trying to do this, did you ever find a solution?
-
noztol almost 8 yearsLooks like as of 2012 no solution for the Workbooks.Count equal to 0?
-
Automate This about 7 yearsThis answer seems to have nothing to do with the question.
-
Chris R. about 7 yearsStill looking for a solution for the he
Workbooks.Count
equal to 0 -
Beginner over 4 yearsIf the opened Excel is in PROTECTED VIEW you may find this answer helpful stackoverflow.com/questions/58458188/…