Danijel Boksan
Danijel Boksan

Reputation: 789

C# Close Excel Instance opened by another application

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))'

Upvotes: 0

Views: 251

Answers (1)

Danijel Boksan
Danijel Boksan

Reputation: 789

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.

Upvotes: 1

Related Questions