Opening Excel and Saving but Excel Instance hangs around

Mar 14, 2013 at 10:05 PM
I am using NetOffice to generate Excel file. But once I close Excep._application objects Excel still stays open and I see in Process Tab. It does not get garbage collected.

Environment : 64bit Windows Server 2008 R2, .Net 3.5 Framework

Code Below:

using Excel = NetOffice.ExcelApi;
        Excel.Application oXL = null;
        Excel._Workbook oWB = null;
        Excel._Worksheet oSheet = null;
        Excel.Range oRng = null;

        try
        {
            //Start Excel and get Application object.
            oXL = new Excel.Application();
            oXL.Visible = bShow;
            oXL.AlertBeforeOverwriting = bShow;
            oXL.Interactive = bShow;
            oXL.DisplayAlerts = bShow;

            //Get a new workbook.
            oWB = (Excel._Workbook)(oXL.Workbooks.Add(Missing.Value));

            oSheet = (Excel._Worksheet)oWB.Sheets[1];
            oSheet.Name = "Shee1";
            FormatSheet(oSheet, "Suggested Trades");

            oSheet = (Excel._Worksheet)oWB.Sheets[2];
            oSheet.Name = "Sheet2";

            oSheet = (Excel._Worksheet)oWB.Sheets[3];
            oSheet.Name = "Shee3";
//Some Processing

//Saving file
                oWB.SaveCopyAs(fileName);
                oWB.Close();
                oXL.Quit();
}
        catch (Exception e)
        {
            return false;
        }
        finally
        {
            //Release Objects
            releaseObject(oRng);
            releaseObject(oSheet);
            releaseObject(oWB);
            releaseObject(oXL);
        }


    private void releaseObject(object obj)
    {
        try
        {
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
            obj = null;
        }
        catch (Exception ex)
        {
            obj = null;
        }
        finally
        {
            GC.Collect();
        }
    }

Help Appreciated
Coordinator
Mar 15, 2013 at 10:18 AM
  1. remove the finaly block
  2. remove the releaseObject method
  3. call oXL.Dispose(); after Quit()
Sebastian
Mar 15, 2013 at 3:58 PM
Thanks; that worked...
Mar 20, 2013 at 5:19 PM
Edited Mar 20, 2013 at 5:20 PM
Hello,

In case you have issues with COM files, always close every process created by them to avoid memory lack.
        private void ManagePPTProcesses()
        {
            Process[] processes = System.Diagnostics.Process.GetProcessesByName("EXCEL");
            foreach (Process p in processes)
                p.Kill();
        }
In that case, if you loop through multiple Excel files in order to import all data, you close them all with the code above.
Apr 5, 2013 at 4:04 PM
mike27015 wrote:
Hello,

In case you have issues with COM files, always close every process created by them to avoid memory lack.
        private void ManagePPTProcesses()
        {
            Process[] processes = System.Diagnostics.Process.GetProcessesByName("EXCEL");
            foreach (Process p in processes)
                p.Kill();
        }
In that case, if you loop through multiple Excel files in order to import all data, you close them all with the code above.
Beware of that: Maybe u r closing unintentionally other excel apps.