?
Solved

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

Posted on 2007-12-06
3
Medium Priority
?
642 Views
Last Modified: 2008-02-01
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

0
Comment
Question by:hayabusa39
3 Comments
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 160 total points
ID: 20425805
please add the following 2 lines after the CleanUp(xxxx) lines:

GC.Collect();
GC.WaitForPendingFinalizers();
0
 
LVL 2

Accepted Solution

by:
mukeshlaku earned 1840 total points
ID: 20426465
Hi

Use code in below

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_objBook.close false;\\ this will close the open workbook file.
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);
m_objExcel.Quit();
cleanUp(m_objExcel);
GC.Collect();
GC.WaitForPendingFinalizers();


0
 

Author Comment

by:hayabusa39
ID: 20427694
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.
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Six Sigma Control Plans

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question