Killing Excel instance

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;
 
J

Juan T. Llibre

Calling System.Runtime.InteropServices.Marshal.ReleaseComObject()
with the Excel instance should let you get what you want.
 
G

George Ter-Saakov

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top