Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove Excel application

Status
Not open for further replies.

jslmvl

Vendor
Jan 26, 2008
268
GB
Hi,

I did all my best to remove Excel:
book.Close(true,miss,miss);
excel.Workbooks.Close();
excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
excel = null;
but from the Windows Task Manager I can see EXCEL.EXE still there until I stoped the program. What I was missing?

Following is the full code:

private void button1_Click(object sender, EventArgs e)
{
Excel.Application excel = new Excel.Application();
Excel.Range range;
object miss = System.Reflection.Missing.Value;
//excel.Visible = true;
string fileName = Application.StartupPath + "\\Book1.xlsx";
Excel.Workbook book = excel.Workbooks.Open(fileName, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss);
Excel.Worksheet sheet = (Excel.Worksheet)book.Worksheets[1];
range = (Excel.Range)sheet.Cells[1, 1];
MessageBox.Show(range.Font.Name.ToString());
MessageBox.Show(range.Font.Size.ToString());
book.Close(true,miss,miss);
excel.Workbooks.Close();
excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
excel = null;
}

Thank you in advance.
 
if Excel.Application implements IDisposable (it will have a member named Dispose()) wrap the code in a using statement to manage the disposal.
Code:
using(var excel = new Excel.Application())
{
   Excel.Range range;
   object miss = System.Reflection.Missing.Value;
   var fileName = Application.StartupPath + "\\Book1.xlsx";
   Excel.Workbook book = excel.Workbooks.Open(fileName, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss);
   Excel.Worksheet sheet = (Excel.Worksheet)book.Worksheets[1];
   range = (Excel.Range)sheet.Cells[1, 1];
   MessageBox.Show(range.Font.Name.ToString());
   MessageBox.Show(range.Font.Size.ToString());
   book.Close(true,miss,miss);
   excel.Workbooks.Close();
   excel.Quit();
}

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Thanks a lot for your help!
Then, how to put those code into
private void button1_Click(object sender, EventArgs e)
{
}
 
copy and paste

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Thak you.
I pasted and got
Error 1 'Microsoft.Office.Interop.Excel.Application': type used in a using statement must be implicitly convertible to 'System.IDisposable'
What I was missing?
 
if Excel.Application implements IDisposable ... wrap the code in a using statement to manage the disposal.
it doesn't implement IDisposable. how long are you waiting before checking the object is (un)marshalled? it may be that GC hasn't performed a cleanup.

If you are monitoring performance/memory usage I would also recommend dotTrace from JetBrains. this is a great tool to have for just these scenarios.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
I think it is a basic job for C sharp to remove Excel since even VB can do this without difficulty...perhaps I was missing some thing very simple...
 
Hi,

I've had this issue before with Excel Interop. The only solution (yes - not strictly best practice) is to do a GC.Collect().

Hope this helps,

Graeme


"Just beacuse you're paranoid, don't mean they're not after you
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top