473,406 Members | 2,371 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,406 software developers and data experts.

Closing Excel Object

Guys

I am writing a program which reads the first cell of many Excel
spreadsheets and then takes some actions. The problem that I am
experiencing is that each Excel instance I create persists in memory,
meaning that after a while the machine freezes. Any help would be
greatfuly recieved.

Regards

Lewis

code:

Excel.Application ExcelObj = new Excel.Application();
Excel.Workbook theWorkbook =
ExcelObj.Workbooks.Open(this.sFileName,0,true,5,"" ,"",true,
Excel.XlPlatform.xlWindows,"\t", false, false,0, false, false, false);
Excel.Range range = ExcelObj.get_Range("A1", "A1");
string firstCell = (string)range.Cells.Value2;
ExcelObj.Visible = true; // I see Excel close
ExcelObj.DisplayAlerts = false;
theWorkbook.Close(false, sFileName, true);
ExcelObj.Workbooks.Close();
ExcelObj.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComO bject(ExcelObj);
System.Runtime.InteropServices.Marshal.ReleaseComO bject(theWorkbook);
GC.Collect();
GC.WaitForPendingFinalizers();

theWorkbook = null;
ExcelObj = null;

Nov 16 '05 #1
2 4937
Hi

You did not release the Excel.Range object. You should release all the COM
wrapped objects.

That should normally do it. I've also experienced that some time Excel
wasn't properly closed even when releasing all the wrapper objects, and
while I cannot offer a solution for that (the project that involves excel is
currently awaiting final approval), here's something that'll certainly work:

Process [] localByName = Process.GetProcessesByName("excel");
foreach (Process proc in localByName)
{
proc.kill();
}

It's certainly not nice, but it'll get the job done.

Regards
Stephan
<la******@gmail.com> wrote in message
news:11*********************@l41g2000cwc.googlegro ups.com...
Guys

I am writing a program which reads the first cell of many Excel
spreadsheets and then takes some actions. The problem that I am
experiencing is that each Excel instance I create persists in memory,
meaning that after a while the machine freezes. Any help would be
greatfuly recieved.

Regards

Lewis

code:

Excel.Application ExcelObj = new Excel.Application();
Excel.Workbook theWorkbook =
ExcelObj.Workbooks.Open(this.sFileName,0,true,5,"" ,"",true,
Excel.XlPlatform.xlWindows,"\t", false, false,0, false, false, false);
Excel.Range range = ExcelObj.get_Range("A1", "A1");
string firstCell = (string)range.Cells.Value2;
ExcelObj.Visible = true; // I see Excel close
ExcelObj.DisplayAlerts = false;
theWorkbook.Close(false, sFileName, true);
ExcelObj.Workbooks.Close();
ExcelObj.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComO bject(ExcelObj);
System.Runtime.InteropServices.Marshal.ReleaseComO bject(theWorkbook);
GC.Collect();
GC.WaitForPendingFinalizers();

theWorkbook = null;
ExcelObj = null;

Nov 16 '05 #2
Stephen

I noticed that I hadn't killed the Range object of and added this to no
avail.

However, your Kill processes method works like a charm.

You are a King amongst men, many thanks!

Lewis

Nov 16 '05 #3

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

Similar topics

6
by: Hema S | last post by:
Hi, I want to use excel object in my asp.net application. When excel application is installed its working fine. When i uninstal the excel application and refer the relevant dll explicitly... i...
2
by: Darren Barrick via .NET 247 | last post by:
Hello All! I am using Office Automation in one of my applications in orderto read/write to an excel spreadsheet from a VB.Net application. I thus create the application with ExcelApplication...
3
by: Bart Filipski | last post by:
Hello, Does anyone know how looks the syntax of SORT method in Excel object ( i mean precisely in Excel.Workbook.Worksheet.Cells.Sort(...) ) I have tryied in VBA and it works great, but I don't...
4
by: Norton | last post by:
I type the following code to open/close an excel appz Dim oExcel As Excel.Application oExcel = new Excel.Application .... .... ... If Not oExcel Is Nothing Then oExcel.Quit() If Not oExcel...
1
by: Vikram | last post by:
excel object not getting killed even after calling System.Runtime.InteropServices.Marshal.ReleaseComObjec. Is there any workaround
0
by: Jono | last post by:
Hello, I've been getting this message when closing excel (not necessarily when closing the workbook by itself, but when closing Excel and the workbook at the same time): ...
3
by: rlntemp-gng | last post by:
RE: Access 2003/Excel 2003 Problem: After I close the Access application completely, I go out to the Task Manager and there is an Excel.exe object still sitting out there. My Access...
2
by: Ronin85 | last post by:
Hi , I recently have much pain working with excel application especially closing excel . I try several method but with no success i try to urge garbage collector to dispose excel application object,...
1
by: Scott M. | last post by:
Many methods return objects when they are called. With Excel these objects are placed in memory and must be destroyed via ReleaseComObject as you have done with your NAR method, but the line: ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.