470,573 Members | 1,735 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,573 developers. It's quick & easy.

excel reference remains open

using the code below (some parts not included), I create a new excel
workbook with spreadheets.
I then want to delete a spreadsheet, but a reference remains open and
excel stays in task manager when
the code highlighted below is used. can someone help me identify what
object remains open and
how to release that reference? thanks

using Excel = Microsoft.Office.Interop.Excel;

Excel.Application xl;
Excel.Worksheet sheet;
Excel.Workbooks wbs;

sheets added, etc

//////////////////// reference remains when this code is used
Excel.Workbook wb = xl.ActiveWorkbook;
Excel.Worksheet sheet = (Excel.Worksheet)wb.Sheets[1];
////////////////////

xl.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComO bject(sheet);
sheet = null;
System.Runtime.InteropServices.Marshal.ReleaseComO bject(wb);
wb = null;
System.Runtime.InteropServices.Marshal.ReleaseComO bject(wbs);
wbs = null;
System.Runtime.InteropServices.Marshal.ReleaseComO bject(xl);
xl = null;
GC.Collect();
GC.WaitForPendingFinalizers();

Apr 17 '07 #1
3 4630
<pl*****************@yahoo.comwrote in message
news:11*********************@e65g2000hsc.googlegro ups.com...
using the code below (some parts not included), I create a new excel
workbook with spreadheets.
I then want to delete a spreadsheet, but a reference remains open and
excel stays in task manager when
the code highlighted below is used. can someone help me identify what
object remains open and
how to release that reference? thanks

using Excel = Microsoft.Office.Interop.Excel;

Excel.Application xl;
Excel.Worksheet sheet;
Excel.Workbooks wbs;

sheets added, etc

//////////////////// reference remains when this code is used
Excel.Workbook wb = xl.ActiveWorkbook;
Excel.Worksheet sheet = (Excel.Worksheet)wb.Sheets[1];
////////////////////

xl.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComO bject(sheet);
sheet = null;
System.Runtime.InteropServices.Marshal.ReleaseComO bject(wb);
wb = null;
System.Runtime.InteropServices.Marshal.ReleaseComO bject(wbs);
wbs = null;
System.Runtime.InteropServices.Marshal.ReleaseComO bject(xl);
xl = null;
GC.Collect();
GC.WaitForPendingFinalizers();

You already posted the same question before, the answer remains the same, you need to
release all references to Excel objects before you Quit, also, you need to Close the
Workbook before Saving and you need to Save before Quitting.
It really makes no sense to post only part of the code, please post a small but complete
sample that illustrates the issue.
You also have to tell us in what context you are running this code, here I mean, from a
client or from a service like application.

Willy.

Apr 17 '07 #2
On Apr 17, 6:26 pm, "Willy Denoyette [MVP]"
<willy.denoye...@telenet.bewrote:
<pleaseexplaintom...@yahoo.comwrote in message

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


using the code below (some parts not included), I create a new excel
workbook with spreadheets.
I then want to delete a spreadsheet, but a reference remains open and
excel stays in task manager when
the code highlighted below is used. can someone help me identify what
object remains open and
how to release that reference? thanks
using Excel = Microsoft.Office.Interop.Excel;
Excel.Application xl;
Excel.Worksheet sheet;
Excel.Workbooks wbs;
sheets added, etc
//////////////////// reference remains when this code is used
Excel.Workbook wb = xl.ActiveWorkbook;
Excel.Worksheet sheet = (Excel.Worksheet)wb.Sheets[1];
////////////////////
xl.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComO bject(sheet);
sheet = null;
System.Runtime.InteropServices.Marshal.ReleaseComO bject(wb);
wb = null;
System.Runtime.InteropServices.Marshal.ReleaseComO bject(wbs);
wbs = null;
System.Runtime.InteropServices.Marshal.ReleaseComO bject(xl);
xl = null;
GC.Collect();
GC.WaitForPendingFinalizers();

You already posted the same question before, the answer remains the same, you need to
release all references to Excel objects before you Quit, also, you need to Close the
Workbook before Saving and you need to Save before Quitting.
It really makes no sense to post only part of the code, please post a small but complete
sample that illustrates the issue.
You also have to tell us in what context you are running this code, here I mean, from a
client or from a service like application.

Willy.- Hide quoted text -

- Show quoted text -
sorry I left out this line sheet.Delete();

//////////////////// reference remains when this code is used
Excel.Workbook wb = xl.ActiveWorkbook;
Excel.Worksheet sheet = (Excel.Worksheet)wb.Sheets[1];
sheet.Delete();
////////////////////

there are no additional references using wb or sheet in the
application.
is there a (hidden?) reference in these 3 lines that isn't handled
here?
xl.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComO bject(sheet);
sheet = null;
System.Runtime.InteropServices.Marshal.ReleaseComO bject(wb);
wb = null;
System.Runtime.InteropServices.Marshal.ReleaseComO bject(wbs);
wbs = null;
System.Runtime.InteropServices.Marshal.ReleaseComO bject(xl);
xl = null;
GC.Collect();
GC.WaitForPendingFinalizers();

and, no this is not the same question that wasn't answered before.
thanks anybody with constructive advice.
Apr 18 '07 #3
<pl*****************@yahoo.comwrote in message
news:11**********************@n76g2000hsh.googlegr oups.com...
On Apr 17, 6:26 pm, "Willy Denoyette [MVP]"
<willy.denoye...@telenet.bewrote:
><pleaseexplaintom...@yahoo.comwrote in message

news:11*********************@e65g2000hsc.googlegr oups.com...


using the code below (some parts not included), I create a new excel
workbook with spreadheets.
I then want to delete a spreadsheet, but a reference remains open and
excel stays in task manager when
the code highlighted below is used. can someone help me identify what
object remains open and
how to release that reference? thanks
using Excel = Microsoft.Office.Interop.Excel;
Excel.Application xl;
Excel.Worksheet sheet;
Excel.Workbooks wbs;
sheets added, etc
//////////////////// reference remains when this code is used
Excel.Workbook wb = xl.ActiveWorkbook;
Excel.Worksheet sheet = (Excel.Worksheet)wb.Sheets[1];
////////////////////
xl.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComO bject(sheet);
sheet = null;
System.Runtime.InteropServices.Marshal.ReleaseComO bject(wb);
wb = null;
System.Runtime.InteropServices.Marshal.ReleaseComO bject(wbs);
wbs = null;
System.Runtime.InteropServices.Marshal.ReleaseComO bject(xl);
xl = null;
GC.Collect();
GC.WaitForPendingFinalizers();

You already posted the same question before, the answer remains the same, you need to
release all references to Excel objects before you Quit, also, you need to Close the
Workbook before Saving and you need to Save before Quitting.
It really makes no sense to post only part of the code, please post a small but complete
sample that illustrates the issue.
You also have to tell us in what context you are running this code, here I mean, from a
client or from a service like application.

Willy.- Hide quoted text -

- Show quoted text -

sorry I left out this line sheet.Delete();

//////////////////// reference remains when this code is used
Excel.Workbook wb = xl.ActiveWorkbook;
Excel.Worksheet sheet = (Excel.Worksheet)wb.Sheets[1];
sheet.Delete();
////////////////////

there are no additional references using wb or sheet in the
application.
is there a (hidden?) reference in these 3 lines that isn't handled
here?
xl.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComO bject(sheet);
sheet = null;
System.Runtime.InteropServices.Marshal.ReleaseComO bject(wb);
wb = null;
System.Runtime.InteropServices.Marshal.ReleaseComO bject(wbs);
wbs = null;
System.Runtime.InteropServices.Marshal.ReleaseComO bject(xl);
xl = null;
GC.Collect();
GC.WaitForPendingFinalizers();

and, no this is not the same question that wasn't answered before.
thanks anybody with constructive advice.


Sorry, but you don't come up with a complete sample, how can we answer your question "is
there a (hidden?) reference in these 3 lines that isn't handled here?", if you don't post
the full code or at least a small but complete sample that illustrates the issue.
Also, please answer the question regarding the context and type of this application, is it a
console like a Windows like application, or is it something that runs from a Service? It's
important to answer these, before one can give "constructive advise".

Willy.


Apr 18 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by Allison Bailey | last post: by
3 posts views Thread by Vagabond Software | last post: by
reply views Thread by rhett | last post: by
7 posts views Thread by =?Utf-8?B?VGVycnkgSG9sbGFuZA==?= | last post: by
1 post views Thread by livre | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.