Link to home
Start Free TrialLog in
Avatar of hayabusa39
hayabusa39

asked on

EXCEL.EXE instance can't be removed after using Excel._QueryTable.Add

I seem to have a unique variation on a common problem that has been addressed many times on this board.

I am experiencing the unable to kill EXCEL.EXE instance when running a client process. I've cleaned my code as previous solutions have suggested, but one section won't allow me to kill the instance even with the recommended solution.

My code is below.

It works for all the excel variable declarations. The instance remains when I execute the line m_objQryTable = m_objQryTables.Add(con, m_objRange, query);. After, there is no getting rid of the EXCEL.EXE.
What is this method allocating that needs to released for me to be able to clear the excel instance?

By the way, this link was a big help when first addressing this issue.
http://support.microsoft.com/default.aspx?scid=kb;en-us;317109
 Thanks.
Excel.Application m_objExcel;
Excel.Workbooks m_objBooks;
Excel._Workbook m_objBook;
Excel.Sheets m_objSheets;
Excel._Worksheet m_objSheet;
Excel.Range m_objRange;
Excel.QueryTables m_objQryTables;
Excel._QueryTable m_objQryTable;
 
System.Reflection.Missing m_objOpt = System.Reflection.Missing.Value;
m_objExcel = new Excel.Application();
m_objBooks = m_objExcel.Workbooks;
m_objBook = m_objBooks.Add(m_objOpt);
m_objSheets = m_objBook.Worksheets;
m_objSheet = (Excel._Worksheet)m_objSheets.get_Item(1);
m_objRange = m_objSheet.get_Range("A1", m_objOpt);
m_objQryTables = m_objSheet.QueryTables;
m_objQryTable = m_objQryTables.Add(con, m_objRange, query);
 
m_objExcel.Quit();
cleanUp(query);
cleanUp(con);
cleanUp(m_objRange);
cleanUp(m_objQryTable);
cleanUp(m_objQryTables);
cleanUp(m_objSheet);
cleanUp(m_objSheets);
cleanUp(m_objBook);
cleanUp(m_objBooks);
cleanUp(m_objExcel);
 
private void cleanUp(object cleanMe){
try{
  System.Runtime.InteropServices.Marshal.ReleaseComObject(cleanMe);
}
catch (Exception ex){}
finally{
  cleanMe = null;
}
}

Open in new window

SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hayabusa39
hayabusa39

ASKER

I gave partial points to angellll for giving part of code, but the solution given by mukeshlaku really nailed it. Thanks both for your help.