Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2007-12-06
3
Medium Priority
?
639 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: 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

Outlook for dependable use in a very small business   This article is about using the Outlook application (part of Microsoft Office) in a very small business, or for homeowners where dependability and reliability are critical requirements. This …
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

636 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