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!

Use of Microsoft Excel hangs - how to clear ?

Status
Not open for further replies.

StevenK

Programmer
Jan 5, 2001
1,294
GB
We're making use of MS Excel within a .NET C# application.
We have an issue whereby on closing the form / application which makes use of this - the instance of Excel is not cleared and still can be seen within 'Task Manager'.

private Excel.Application oXL;
private Excel.Workbook oWB;
private Excel.Worksheet oSheet;

We're making use of code like :
oXL = new Excel.ApplicationClass();
oWB = oXL.Workbooks.Add(xlStr);
oSheet = (Excel.Worksheet) sheets.get_Item(1);

On closing of the form we're using code such as :
if (oSheet != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(oSheet);
}
if (oWB != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(oWB);
}
if (oXL != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(oXL);
}

However hard we try - we can't seem to get 'EXCEL.EXE' to be removed from the 'Task Manager'

Previously when we had a problem with MS Word we resolved it in a similar to above. This doesn't seem to work for EXCEL mind you. :(

Can anyone help us out with this one ?

Thanks in advance
Steve
 
Here is how I used and there is no EXcel.exe in Task Manager in Release mode but there are under Debug:

// Start
GC.Collect();
m_Excel = new Excel.Application();
m_WorkBook= m_Excel.Workbooks.Add(m_MissingValue);
Excel._Worksheet w = (Excel._Worksheet)m_WorkBook.Worksheets.get_Item(1); // or whatever

// Populate sheet

m_WorkSheet=(m_WorkBook.SaveAs(...);

//Finishing

m_WorkBook.Close(null, null, null);
m_Excel.Quit();
if (m_Excel!=null) System.Runtime.InteropServices.Marshal.ReleaseComObject(m_Excel);
if (m_WorkSheet!=null) System.Runtime.InteropServices.Marshal.ReleaseComObject(m_WorkSheet);
if (m_WorkBook!=null) System.Runtime.InteropServices.Marshal.ReleaseComObject(m_WorkBook);

m_WorkSheet=null;
m_WorkBook=null;
m_Excel=null;
GC.Collect();

Maybe there is another way but it is working Okay in Realease.
-obislavu-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top