Solved

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

Posted on 2007-12-06
3
627 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 40 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 460 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Microsoft Office Picture Manager is not included in Office 2013. This comes as a shock to users upgrading from earlier versions of Office, such as 2007 and 2010, where Picture Manager was included as a standard application. This article explains how…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

696 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