Killing Excel instance

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi! All,

I am using a ASP.NEt application, wherein i am automating Execl object. I
am not able to destroy the instance, eve if I write this to destroy the
instance of Excel. Please find the code below

GC.Collect(0);
myBook.Close(null,null,null);
myApp.Workbooks.Close();

myApp.Quit();

mySheet = null;
myBook = null;
myApp = null;
 
You have to use System.Runtime.InteropServices.Marshal.ReleaseComObject to
manually release COM objects such as Excel.
Garbage collector will release it eventually but you never know when.

----------------------------------

Also be careful (very careful) with statements like that
myApp.Workbooks.Close();

Behind the scene compiler does following

tmp = myApp.Workbooks
tmp.Close()

So the variable tmp is a COM object and will not be released. Bottom line,
you should never have more than one dot in your statement when work with COM
objects.

---------------------------------------

Here is your code rewritten that should close Excel. (it's a pseducode)

myBook.Close(null,null,null);
Marshal.ReleaseComObject(myBook);
myBook = null;
tmp = myApp.Workbooks
tmp.Close();
Marshal.ReleaseComObject(tmp);
tmp = null;
myApp.Quit();
Marshal.ReleaseComObject (myApp);
myApp = null;



George.
 
Back
Top