473,327 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

How to properly close Excel when finished?

Hi, I've written an application to do a while bunch of Excel automation.
I open a file, scan through all the worksheets, extract data, and then I
try to close Excel. However, I can see that I'm not doing it
effectively. If I am debugging my app, and I kill the app before my app
exits, but after the call that should close excel, I still have an
excell process sitting around. After about 20 + of those stack up, you
start to feel it.
If I quit all the way out of my app, somehow Excel DOES get cleaned up
and the instance / process is closed.

So, how do I properly close an instance of excel?

Thank you!
Currently doing it this way:

public void ExtractDataFromExcel(string fileName)
{

// Instantiate an Excel control object
ExcelObj = new Excel.Application();

// Disable alert messages
ExcelObj.DisplayAlerts = false;

// See if the Excel Application Object was successfully constructed
if (ExcelObj == null)
{
MessageBox.Show("ERROR: EXCEL couldn't be started!");
System.Windows.Forms.Application.Exit();
}

theWorkbook = ExcelObj.Workbooks.Open(
fileName, // Filename
0, // Update links
true, // Read only
5, // Format
"", // Password
"", // Write res password
true, // Ignore read-only recommend
Excel.XlPlatform.xlWindows, // Origin
"\t", // Delimiter (\t = tab delimited)
false, // Editable
false, // Notify
0, // Converter
false, // AddToMru
0, // Local
true // Corrupt load
);

// get the collection of sheets in the workbook
sheets = theWorkbook.Worksheets;
{ ... do some work ... }

// Close Excel workbook
theWorkbook.Close(false, fileName, 0);

object saveChanges = Missing.Value;
object originalFormat = Missing.Value;
object routeDocument = Missing.Value;

ExcelObj.Quit(); // ref saveChanges, ref originalFormat, ref
routeDocument);
} // end of method
Meet people for friendship, contacts,
or romance using free instant messaging software! See a picture you
like? Click once for a private conversation with that person!

www.SOCIALNETWORK.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #1
3 8352
Ah yes... I rembmer thins one... ran into it a couple years ago.

MS was kind enough to come up with a fix for me :)

http://support.microsoft.com/default...b;en-us;266088

Turns out the garbage collection on excel wasn't working right.
Hope that helps!

"David Berman" <da**********@bose.com> wrote in message
news:u$**************@TK2MSFTNGP11.phx.gbl...
Hi, I've written an application to do a while bunch of Excel automation.
I open a file, scan through all the worksheets, extract data, and then I
try to close Excel. However, I can see that I'm not doing it
effectively. If I am debugging my app, and I kill the app before my app
exits, but after the call that should close excel, I still have an
excell process sitting around. After about 20 + of those stack up, you
start to feel it.
If I quit all the way out of my app, somehow Excel DOES get cleaned up
and the instance / process is closed.

So, how do I properly close an instance of excel?

Thank you!
Currently doing it this way:

public void ExtractDataFromExcel(string fileName)
{

// Instantiate an Excel control object
ExcelObj = new Excel.Application();

// Disable alert messages
ExcelObj.DisplayAlerts = false;

// See if the Excel Application Object was successfully constructed
if (ExcelObj == null)
{
MessageBox.Show("ERROR: EXCEL couldn't be started!");
System.Windows.Forms.Application.Exit();
}

theWorkbook = ExcelObj.Workbooks.Open(
fileName, // Filename
0, // Update links
true, // Read only
5, // Format
"", // Password
"", // Write res password
true, // Ignore read-only recommend
Excel.XlPlatform.xlWindows, // Origin
"\t", // Delimiter (\t = tab delimited)
false, // Editable
false, // Notify
0, // Converter
false, // AddToMru
0, // Local
true // Corrupt load
);

// get the collection of sheets in the workbook
sheets = theWorkbook.Worksheets;
{ ... do some work ... }

// Close Excel workbook
theWorkbook.Close(false, fileName, 0);

object saveChanges = Missing.Value;
object originalFormat = Missing.Value;
object routeDocument = Missing.Value;

ExcelObj.Quit(); // ref saveChanges, ref originalFormat, ref
routeDocument);
} // end of method
Meet people for friendship, contacts,
or romance using free instant messaging software! See a picture you
like? Click once for a private conversation with that person!

www.SOCIALNETWORK.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 15 '05 #2
Thanks for the link, but it's specifically for a Java solution. Does
anyone have an equivalent for C#?

David


Meet people for friendship, contacts,
or romance using free instant messaging software! See a picture you
like? Click once for a private conversation with that person!

www.SEN.us

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #3
David,
Make sure you call Marshal.ReleaseComObject on each Excel object you
reference in your code and set them all to null/Nothing afterwards.

Ron Allen
"David Berman" <da**********@bose.com> wrote in message
news:e2**************@TK2MSFTNGP10.phx.gbl...
Thanks for the link, but it's specifically for a Java solution. Does
anyone have an equivalent for C#?

David


Meet people for friendship, contacts,
or romance using free instant messaging software! See a picture you
like? Click once for a private conversation with that person!

www.SEN.us

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Lorenzo Melato | last post by:
Hi everyone, I have a very strange problem. If I open a .NET application from EXCEL using SHELL function, when I close EXCEL, the EXCEL.EXE process remain active and I must close it by Task...
0
by: Stewart Allen | last post by:
Hi there, I've got some code to open up an Excel workbook but what I want is for the code to pause until the Excel workbook is closed. Sub EditWorkbook(strFile as String) Dim xlApp As...
5
by: Wenke Ji | last post by:
Hi I open a Excel workbook using below API: Set ExcelServer = CreateObject("EXCEL.Application") Set TargetWorkbook = ExcelServer.Workbooks.Open (CurrentBook) Befor the programm exit , I use...
35
by: Eric Sabine | last post by:
In my Finally block, I was using cn.close (where cn is an ADO.NET connection object, SQLConnection to be exact) and then I came across the following in some microsoft code. If Not cn Is Nothing...
6
by: icony | last post by:
Hi everyone, Here's my problem. I have a report that i want to use with the command openreport. The report open and close in a fraction of a second. I cant see why i cant view the thing. Cause i...
13
by: chuckie_9497 | last post by:
hello all you gurus. I am struggling with releasing com objects. I have isolated the problem to the code below. Objects are released and the process ends until I use "int k = sheet.Count;" Then...
4
by: Yoduh | last post by:
Hello, I'm fairly new to javascript. I'm making a website for my personal use that will read from Excel and display some data on an HTML page and I've been learning javascript as I go. A problem...
1
by: JDeats | last post by:
So I have a reference to Microsoft Office 10.0 Object Library in my WinForms project (VS.NET 2005, 2.0 Framework) using Microsoft.Office.Core; using Excel; I have an instance of...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.