none
Clean up after opening excel from C#/.NET application(excel.exe process not terminating) RRS feed

  • Question

  • We are opening an excel file from our application and display that to the user

    Microsoft.Office.Interop.Excel.Application ExcelApp;
    try
    {
    ExcelApp=  (Microsoft.Office.Interop.Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
    }
    catch
    {           
    ExcelApp = return new Microsoft.Office.Interop.Excel.Application();
    }
    
    ExcelApp.Visible = true;
    ExcelApp.WindowState = XlWindowState.xlMaximized; 
    
    var wrkBooks = ExcelApp.Workbooks;var wrkBook = wrkBooks.Open(fileName, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "",                                         true, false, 0, true, false, false);
    
    Marshal.ReleaseComObject(wrkBooks);
    Marshal.ReleaseComObject(wrkBook);
    This works and opens the excel file for the user. However, when the user closes the excel file, excel.exe still remains in memory. 

    Probably the application still has some references to excel. How to make sure we remove all references to excel from the .net app?

    Tuesday, October 7, 2014 2:24 PM

Answers

  • Do you have any add-ins installed for Excel? Other add-ins may not release underlying COM objects also.

    Did you try to decrease the reference counter for the Application object, i.e. call the Marshal.ReleaseComObject(ExcelApp)?

    • Marked as answer by varghesejim Tuesday, October 7, 2014 3:06 PM
    Tuesday, October 7, 2014 2:37 PM

All replies

  • Do you have any add-ins installed for Excel? Other add-ins may not release underlying COM objects also.

    Did you try to decrease the reference counter for the Application object, i.e. call the Marshal.ReleaseComObject(ExcelApp)?

    • Marked as answer by varghesejim Tuesday, October 7, 2014 3:06 PM
    Tuesday, October 7, 2014 2:37 PM
  • Also, if handing over complete control of the app to the user, before releasing your last reference

    ExcelApp.UserControl = True;

    Tuesday, October 7, 2014 2:58 PM
    Moderator
  • Thanks, will do.
    Tuesday, October 7, 2014 3:06 PM