By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,876 Members | 1,191 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,876 IT Pros & Developers. It's quick & easy.

excel reference remains open

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
<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

P: n/a
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

P: n/a
<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.