0

I have a real pain of an issue whereas the Excel.Application() does not want to get released, no matter what. Even after a new class is instantiated and disposed immediately afterwards, it still appears in the process list.

if (_ExcelApp == null)
    _ExcelApp = new Microsoft.Office.Interop.Excel.Application();
Dispose();

public void Dispose()
{
    if (_ExcelApp != null)
    {
        try
        {
            _ExcelApp.Quit();
            Marshal.FinalReleaseComObject(_ExcelApp);
        }
        catch (Exception) { }
            _ExcelApp = null;
    }
}

Please help!

Rudolf Lamprecht
  • 1,050
  • 1
  • 14
  • 37
  • 1
    [Related?](http://stackoverflow.com/questions/27930307/why-does-microsoft-office-interop-excel-application-quit-leave-the-background) – stuartd Nov 01 '16 at 11:35
  • Check this [question](http://stackoverflow.com/questions/9962157/safely-disposing-excel-interop-objects-in-c) – shadow Nov 01 '16 at 11:41
  • Check this http://stackoverflow.com/questions/158706/how-to-properly-clean-up-excel-interop-objects – Turay Melo Nov 01 '16 at 11:48
  • the best way to dispose of something is to use a using() statement this automatically disposes of objects once they are finished with them – Simon Price Nov 01 '16 at 11:55
  • Ok, so I've mangaged to set the `_ExcelApp` and dispose of it immediately. However, If I do `_ExcelApp.Workbooks.Add()` (note no parameter) and then try to expose of `_ExcelApp`, it keeps holding on to the application. – Rudolf Lamprecht Nov 01 '16 at 11:58

1 Answers1

4

It do not release because not all COM objects which are related to _ExcelApp released. If you provide all piece of code, it may be more clearer.

Workbooks wb =_ExcelApp.Workbooks;
Workbook book = wb.Add();

Then in try catch:

          try
            {
              book.Close();
              Marshal.ReleaseComObject(book);
              wb.Close();
              Marshal.ReleaseComObject(wb);
              _ExcelApp.Quit();
              Marshal.FinalReleaseComObject(_ExcelApp);
            }
        catch (Exception) { }
            _ExcelApp = null;
    }

_ExcelApp.WorkBooks.Add(); create new WorkBook and you do not release this object. Also it create Workbooks object which also have to be released.

As I have mentioned earlier will be better if you show all code(if it is possible of course)

mojo
  • 174
  • 1
  • 2
  • 9
  • So I ended up releasing each and every COM object after they have been used and setting them to null. It turned out to be a big hassle as I had several ranges of columns and cells, worksheets, workbooks, etc. – Rudolf Lamprecht Nov 02 '16 at 12:09
  • Might I add that I did not set the objects to null originally, so that probably was my flaw. Ta mojo – Rudolf Lamprecht Nov 02 '16 at 12:15