3

I try to get Excel application in my code with this method:

Excel.Application xlApp = GetApplication();
if (xlApp == null) xlApp = new Excel.Application();

where

private Excel.Application GetApplication()
    {
        Excel.Application result = null;

        try
        {
            result = (Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
        }
        catch (System.Runtime.InteropServices.COMException ex)
        {
            //Excel is not open
        }

        return result;
    }

but the System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application") always throws exception, even when Excel application is open.

Exception: HRESULT: 0x800401E3

StackTrace:

   in System.Runtime.InteropServices.Marshal.GetActiveObject(Guid& rclsid, IntPtr reserved, Object& ppunk)
   in System.Runtime.InteropServices.Marshal.GetActiveObject(String progID)
   in RaceToolTests.UnitTest1.GetApplication() in C:\Users\...
  • Are you using _only_ Interop or are you also using ClosedXML perhaps? Do you have a line `using Excel = Microsoft.Office.Interop.Excel;` I am asking because your code works perfectly fine for me. – LocEngineer Sep 18 '17 at 13:54
  • I'm using only Interop. In my code I added:`using Excel = Microsoft.Office.Interop.Excel;` – KentuckyJack92 Sep 18 '17 at 14:00
  • 1
    Does your Excel version match the Interop version, e.g. 14.0 for Excel 2010 or 16.0 for 2016? What version are you using? – LocEngineer Sep 18 '17 at 14:03
  • Have you tried the solution from here - https://stackoverflow.com/questions/6682678/accessing-an-open-excel-workbook-in-c-sharp? Yours is really close to it. – Vityata Sep 18 '17 at 14:04
  • @LocEngineer The version was wrong, I corrected it but the result is the same :( – KentuckyJack92 Sep 18 '17 at 14:12
  • Probably still wrong. Maybe using 64 instead of 32 bit version? Like I said: code itself works perfectly fine. – LocEngineer Sep 18 '17 at 14:13
  • @Vityata Thanks but that's a different problem, my code throws exception and app returns null. – KentuckyJack92 Sep 18 '17 at 14:14
  • @LocEngineer I'm working with unit test, i can't change the version from 32 to 64bit. – KentuckyJack92 Sep 18 '17 at 14:19
  • 2
    The exception simply means that it could not find a running instance. Which is a pretty likely outcome, the problem is that there might be multiple running instances and you always really care which one you connect to. You'll need to read the [KB artlcle](https://support.microsoft.com/en-us/help/238610/getobject-or-getactiveobject-cannot-find-a-running-office-application) that discusses the behavior. – Hans Passant Sep 18 '17 at 14:21

2 Answers2

1

Yes it is possible to not create a new instance of Excel. Use below to get existing instance of Excel app.

var path = @"C:\Temp";
var excelFileName = $@"{path}\Sample.xlsx";

var excelApp = System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application") as Excel.Application;
// Make the object visible.
excelApp.Visible = true;

// open workbook
Excel.Workbook xlWorkbook = excelApp.Workbooks.Open(excelFileName);
Mark
  • 2,175
  • 20
  • 23
0

Please try following code:

//import
using Excel = Microsoft.Office.Interop.Excel;

//define
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;

private void openXL()
{

    string fileName = "PATH\\FILE_NAME.xlsx"; ;

    xlApp = new Excel.Application();
    xlWorkBook = xlApp.Workbooks.Open(fileName);
    xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
}
imsome1
  • 1,182
  • 4
  • 22
  • 37
  • I already have one instance of Excel open. I would like to get that instance instead of opening a new one. – KentuckyJack92 Sep 18 '17 at 13:43
  • is your "xlApp" public or global? if it is global or public you can use that – imsome1 Sep 18 '17 at 13:48
  • maybe I made a confusing question. The point is that I need to programmatically update a file that must be open in Excel without changing its path. To do that, I thought to close, save and re-open it in Excel. That's the reason why I would like a way to identify the Excel instance that hosts the file I'm interested in (maybe among various different open instances of Excel). – KentuckyJack92 Sep 18 '17 at 13:59