0

I'm wondering if some other software create Excel instance, is it possible from c# to access that instance and close but without killing the process ? I had tried with Marshal.GetActiveObject("Excel.Application") but that is throwing me the error:

System.Runtime.InteropServices.COMException: 'Operation unavailable (Exception from HRESULT: 0x800401E3 (MK_E_UNAVAILABLE))'
Kit
  • 20,354
  • 4
  • 60
  • 103
Danijel Boksan
  • 779
  • 1
  • 13
  • 30

1 Answers1

1

I found a way to make it work. I had used

using Excel  = Microsoft.Office.Interop.Excel.Application;

later in code I used:

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

And this had created me a problem. I changed the code to:

using Microsoft.Office.Interop.Excel.Application;

and

var obj = (Microsoft.Office.Interop.Excel.Application)Marshal.GetActiveObject("Excel.Application");
obj.Quit();

And now this is working normally.

Kit
  • 20,354
  • 4
  • 60
  • 103
Danijel Boksan
  • 779
  • 1
  • 13
  • 30
  • 1
    So in the first example you are NOT calling `.Quit()` in the second you are calling `.Quit()` and are surprised why the first example doesn't end the excel application? – Rand Random Jul 16 '21 at 12:36
  • For sure later I had add `.Quit().` before I had exception on first line `var app = (Excel.Application)Marshal.GetActiveObject("Excel.Application"); ` -> This line had create me exception from question above... P.S. I was not ask why application is not ending, I had ask how to access to already open instance of Excel generated by another system. – Danijel Boksan Jul 16 '21 at 13:46