closing excel from c#

  • Thread starter Thread starter pleaseexplaintome_2
  • Start date Start date
P

pleaseexplaintome_2

Help please. The excel instance is removed from task manager when I
run the code below. If I uncomment the lines pertaining to a
workbook, the instance of excel is not removed from task manager. can
someone help? Thanks

Excel.Application xl1 = new Excel.Application();
//Microsoft.Office.Interop.Excel.Workbook wb1 =
xl1.Workbooks.Add(System.Reflection.Missing.Value);
xl1.Visible = true;
xl1.Quit();
//System.Runtime.InteropServices.Marshal.ReleaseComObject(wb1);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xl1);
//wb1 = null;
xl1 = null;
GC.Collect();
GC.WaitForPendingFinalizers();
 
You have to be more aware of the objects that you are using in Excel.
For example, you are creating the application, and you are releasing that.
However, you are exposing the Workbooks property, which you have to release
as well. Then you have to release the workbook that you have (which you are
doing).

Every property that returns an object, for the most part, you have to
keep track of and release when you are done (through a call to
ReleaseComObject). Then, when you call quit, and then ReleaseComObject on
the application, Excel should close.

Hope this helps.
 
You have to be more aware of the objects that you are using in Excel.
For example, you are creating the application, and you are releasing that.
However, you are exposing the Workbooks property, which you have to release
as well. Then you have to release the workbook that you have (which you are
doing).

Every property that returns an object, for the most part, you have to
keep track of and release when you are done (through a call to
ReleaseComObject). Then, when you call quit, and then ReleaseComObject on
the application, Excel should close.


Thanks for the response....and I understand what u r saying....but how
do I release the workbook object?
System.Runtime.InteropServices.Marshal.ReleaseComObject(wb1);
wb1 = null;
doesn't do it.
Thanks
 
You also have to release the workbooks object (note the plural) that is
exposed by the workbooks property.
 
Help please. The excel instance is removed from task manager when I
run the code below. If I uncomment the lines pertaining to a
workbook, the instance of excel is not removed from task manager. can
someone help? Thanks

Excel.Application xl1 = new Excel.Application();
//Microsoft.Office.Interop.Excel.Workbook wb1 =
xl1.Workbooks.Add(System.Reflection.Missing.Value);
xl1.Visible = true;
xl1.Quit();
//System.Runtime.InteropServices.Marshal.ReleaseComObject(wb1);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xl1);
//wb1 = null;
xl1 = null;
GC.Collect();
GC.WaitForPendingFinalizers();


You need to release *all* references to the Excel COM Interfaces (ReleaseComObject), force
a GC and wait for a finalizer run.
Try following in a console application, it should work.

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

{
static void Main() {
xl.Application xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
xl.Workbook wb = exApp.Workbooks.Add(System.Reflection.Missing.Value);
xlApp.Visible = true;
System.Threading.Thread.Sleep(1000);
exApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(wb);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
GC.Collect();
GC.WaitForPendingFinalizers();
// keep the client running, excell should be gone at this point
System.Threading.Thread.Sleep(30000);
}
...


Willy.
 
You need to release *all* references to the Excel COM Interfaces (ReleaseComObject), force
a GC and wait for a finalizer run.
Try following in a console application, it should work.

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

{
static void Main() {
xl.Application xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
xl.Workbook wb = exApp.Workbooks.Add(System.Reflection.Missing.Value);
xlApp.Visible = true;
System.Threading.Thread.Sleep(1000);
exApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(wb);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
GC.Collect();
GC.WaitForPendingFinalizers();
// keep the client running, excell should be gone at this point
System.Threading.Thread.Sleep(30000);}

..

Willy.- Hide quoted text -

- Show quoted text -

Thanks for all your help. The problem was, as you said all along, I
had a reference I was not releasing.
Now I do the following (try to close wb without saving), but the
wb.close statement causes the app to stay in task manager as does the
wb.SaveAs. Any suggestions? Thank you.

wb.Close(Missing.Value, Missing.Value, Missing.Value);
wb.SaveAs("c:\\test3.xls",
Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing,
Excel.XlSaveAsAccessMode.xlNoChange,
Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Missing.Value);

xl.Quit();

System.Runtime.InteropServices.Marshal.ReleaseComObject(range);

System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);

System.Runtime.InteropServices.Marshal.ReleaseComObject(wb);

System.Runtime.InteropServices.Marshal.ReleaseComObject(xl);
 
Thanks for all your help. The problem was, as you said all along, I
had a reference I was not releasing.
Now I do the following (try to close wb without saving), but the
wb.close statement causes the app to stay in task manager as does the
wb.SaveAs. Any suggestions? Thank you.

wb.Close(Missing.Value, Missing.Value, Missing.Value);
wb.SaveAs("c:\\test3.xls",
Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing,
Excel.XlSaveAsAccessMode.xlNoChange,
Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Missing.Value);

xl.Quit();

System.Runtime.InteropServices.Marshal.ReleaseComObject(range);

System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);

System.Runtime.InteropServices.Marshal.ReleaseComObject(wb);

System.Runtime.InteropServices.Marshal.ReleaseComObject(xl);



You have to close after you have saved the wb.
Also you need to look at the object model for Excel and Office before you start coding in
the wild.
And, you need to add some error handling to your code, errors like above should throw an
Exception, don't know why you didn't mention this, you aren't running this from a service
like IIS do you?

Willy.
 
Back
Top