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?
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.
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With