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!

5
  • 1
    Related?
    – stuartd
    Nov 1, 2016 at 11:35
  • Check this question
    – shadow
    Nov 1, 2016 at 11:41
  • Check this stackoverflow.com/questions/158706/…
    – Turay Melo
    Nov 1, 2016 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 Nov 1, 2016 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. Nov 1, 2016 at 11:58

1 Answer 1

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)

2
  • 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. Nov 2, 2016 at 12:09
  • Might I add that I did not set the objects to null originally, so that probably was my flaw. Ta mojo Nov 2, 2016 at 12:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.