excel reference remains open

  • Thread starter pleaseexplaintome_2
  • Start date
P

pleaseexplaintome_2

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.ReleaseComObject(sheet);
sheet = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(wb);
wb = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(wbs);
wbs = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(xl);
xl = null;
GC.Collect();
GC.WaitForPendingFinalizers();
 
W

Willy Denoyette [MVP]

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.ReleaseComObject(sheet);
sheet = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(wb);
wb = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(wbs);
wbs = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(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.
 
P

pleaseexplaintome_2

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.ReleaseComObject(sheet);
sheet = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(wb);
wb = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(wbs);
wbs = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(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.ReleaseComObject(sheet);
sheet = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(wb);
wb = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(wbs);
wbs = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(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.
 
W

Willy Denoyette [MVP]

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.ReleaseComObject(sheet);
sheet = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(wb);
wb = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(wbs);
wbs = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(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.ReleaseComObject(sheet);
sheet = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(wb);
wb = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(wbs);
wbs = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(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.
 

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

Top