Solved

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

Posted on 2007-12-06
3
614 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 142

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article will show you how to use shortcut menus in the Access run-time environment.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
The viewer will learn how to  create a slide that will launch other presentations in Microsoft PowerPoint. In the finished slide, each item launches a new PowerPoint presentation and when each is finished it automatically comes back to this slide: …
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now