473,385 Members | 1,370 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

closing excel from c#

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.ReleaseComO bject(wb1);
System.Runtime.InteropServices.Marshal.ReleaseComO bject(xl1);
//wb1 = null;
xl1 = null;
GC.Collect();
GC.WaitForPendingFinalizers();

Apr 12 '07 #1
6 23971
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<pl*****************@yahoo.comwrote in message
news:11********************@p77g2000hsh.googlegrou ps.com...
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.ReleaseComO bject(wb1);
System.Runtime.InteropServices.Marshal.ReleaseComO bject(xl1);
//wb1 = null;
xl1 = null;
GC.Collect();
GC.WaitForPendingFinalizers();

Apr 12 '07 #2
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.ReleaseComO bject(wb1);
wb1 = null;
doesn't do it.
Thanks

Apr 12 '07 #3
You also have to release the workbooks object (note the plural) that is
exposed by the workbooks property.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<pl*****************@yahoo.comwrote in message
news:11**********************@y80g2000hsf.googlegr oups.com...
> 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.ReleaseComO bject(wb1);
wb1 = null;
doesn't do it.
Thanks

Apr 13 '07 #4
<pl*****************@yahoo.comwrote in message
news:11********************@p77g2000hsh.googlegrou ps.com...
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.ReleaseComO bject(wb1);
System.Runtime.InteropServices.Marshal.ReleaseComO bject(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.Valu e);
xlApp.Visible = true;
System.Threading.Thread.Sleep(1000);
exApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComO bject(wb);
System.Runtime.InteropServices.Marshal.ReleaseComO bject(xlApp);
GC.Collect();
GC.WaitForPendingFinalizers();
// keep the client running, excell should be gone at this point
System.Threading.Thread.Sleep(30000);
}
...
Willy.

Apr 13 '07 #5
On Apr 13, 5:13 am, "Willy Denoyette [MVP]"
<willy.denoye...@telenet.bewrote:
<pleaseexplaintom...@yahoo.comwrote in message

news:11********************@p77g2000hsh.googlegrou ps.com...


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.ReleaseComO bject(wb1);
System.Runtime.InteropServices.Marshal.ReleaseComO bject(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.Valu e);
xlApp.Visible = true;
System.Threading.Thread.Sleep(1000);
exApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComO bject(wb);
System.Runtime.InteropServices.Marshal.ReleaseComO bject(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.ReleaseComO bject(range);

System.Runtime.InteropServices.Marshal.ReleaseComO bject(sheet);

System.Runtime.InteropServices.Marshal.ReleaseComO bject(wb);

System.Runtime.InteropServices.Marshal.ReleaseComO bject(xl);

Apr 13 '07 #6
<pl*****************@yahoo.comwrote in message
news:11**********************@n59g2000hsh.googlegr oups.com...
On Apr 13, 5:13 am, "Willy Denoyette [MVP]"
<willy.denoye...@telenet.bewrote:
><pleaseexplaintom...@yahoo.comwrote in message

news:11********************@p77g2000hsh.googlegro ups.com...


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.ReleaseComO bject(wb1);
System.Runtime.InteropServices.Marshal.ReleaseComO bject(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.Valu e);
xlApp.Visible = true;
System.Threading.Thread.Sleep(1000);
exApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComO bject(wb);
System.Runtime.InteropServices.Marshal.ReleaseComO bject(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.ReleaseComO bject(range);

System.Runtime.InteropServices.Marshal.ReleaseComO bject(sheet);

System.Runtime.InteropServices.Marshal.ReleaseComO bject(wb);

System.Runtime.InteropServices.Marshal.ReleaseComO bject(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.

Apr 13 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: federico | last post by:
When I close an excel application opened from a python program with .Close() methood , in tasks manager is still present 'excel.exe' process and if I open manually an excel applicaion I have a...
2
by: lasmit42 | last post by:
Guys I am writing a program which reads the first cell of many Excel spreadsheets and then takes some actions. The problem that I am experiencing is that each Excel instance I create persists...
2
by: Mark | last post by:
Hi all, as many had, i have some problems terminating the excel-process. I implemented the solutions i found in this forums. Tried most combinations of them ;-). It still doesn't work. I'll...
2
by: Atley | last post by:
I have written an application that exports data from SQL to Excel. It all works perfectly except that if you open the Task Manager after running my application, there is an instance of Excel in...
2
by: ridawg | last post by:
Hey, I have some code where I open up Excel then loop through several cases to update several workbooks. Basically something like this (not showing all the code just basic structure). Try...
0
by: Jono | last post by:
Hello, I've been getting this message when closing excel (not necessarily when closing the workbook by itself, but when closing Excel and the workbook at the same time): ...
4
by: Yoduh | last post by:
Hello, I'm fairly new to javascript. I'm making a website for my personal use that will read from Excel and display some data on an HTML page and I've been learning javascript as I go. A problem...
1
by: fakehitswizard | last post by:
this is the correct way to close excel with C#. I've seen alot of other bogus posts ALL over the web that don't work, how frustrating. string savepath; bool foundPID; int ourPID = 0; int...
2
by: Ronin85 | last post by:
Hi , I recently have much pain working with excel application especially closing excel . I try several method but with no success i try to urge garbage collector to dispose excel application object,...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.