473,406 Members | 2,549 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.

Excel.EXE won't GO away

I've been using excel within VB.NET applications and I can't get it to close down and remove itself from memory.

I'm using Visual Studio 2003 ver 7.1.3088, Framework 1.1, MS Office 2003 with MS VS Tools for MS Office System 69586-3

After closing all workbooks etc and setting them to nothing, I run the following code to get rid of the Excel Application object

If Not (xlApp Is Nothing) Then
xlApp.Quit()
xlApp = Nothing
GC.Collect()
End If

This does set the application object to nothing in the locals panel but the Excel.EXE process is still running in the Task Manager.

Thanks for your help.
Nov 22 '05 #1
2 3310
Bob
Duncan Allen <Duncan Al***@discussions.microsoft.com> wrote in message news:<C6**********************************@microso ft.com>...
I've been using excel within VB.NET applications and I can't get it to close down and remove itself from memory.


I ran into this in c#. I found a ms web page with a great example on
this but can't find it again now.

You are probably creating some objects implicitly by doing something
like xlApp.Workbook.Worksheet and these objects are not getting freed.
Here are some tips :

1) Create all excel ojbects explicitly
2) Quit Excel
3) Use Marshal.ReleaseComObject on all excel objects
4) Set all excel objects to null

The instance of Excel your code creates should then terminate as you
expect it to.

I hope that helps.

Bob

Here is some sample (c# code)

using System;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
using System.Runtime.InteropServices;

private void Export(string saveToName)
{

//Create all Excel objects
Excel.Application oXL;
Excel.Workbook oWB;
Excel.Worksheet oSheet;
Excel.Range oRng;

try
{
//Start Excel and get Application object.
oXL = new Excel.Application();
oXL.DisplayAlerts = false;
oXL.ScreenUpdating = false;

//Make sure Excel is hidden
oXL.Visible = false;
oXL.UserControl = false;

//Open the workbook.
oWB = oXL.Workbooks.Open(excelFileName,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing);

oSheet = (Excel.Worksheet) oWB.Worksheets.get_Item(1);

oSheet.Activate();

//Copy data from data reader to excel
string[] cell = new
string[30]{"A","B","C","D","E","F","G","H","I","J","K","L"," M","N","O","P","Q","R","S","T","U","V","W","X","Y" ,"Z","AA","AB","AC","AD"};

int row = 2;

while (dReader.Read())
{
for (int index = 0; index < 30; index++)
{ {
oRng = oSheet.get_Range(cell[index] + row.ToString(),
cell[index] + row.ToString());

if (dReader[index].ToString() == "")
{
oRng.Value2 = "0";
}
else
{
oRng.Value2 = dReader[index].ToString();
}

//Release the range object
Marshal.ReleaseComObject(oRng);
}

row++;
}

//Save the file
object fileName = saveToName;

oWB.SaveAs(fileName,
Excel.XlFileFormat.xlExcel9795,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Excel.XlSaveAsAccessMode.xlNoChange,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing);

oXL.Quit();
//Free the remaining excel resources
Marshal.ReleaseComObject(oRng);
Marshal.ReleaseComObject(oSheet);
Marshal.ReleaseComObject(oWB);
Marshal.ReleaseComObject(oXL);

oXL = null;
oWB = null;
oSheet = null;
oRng = null;
}
catch( Exception theException )
{
String errorMessage = "Error: ";
errorMessage = String.Concat(errorMessage, theException.Message);
errorMessage = String.Concat(errorMessage, " Line: ");
errorMessage = String.Concat(errorMessage, theException.Source);

MessageBox.Show(errorMessage, "Error");
}
}
Nov 22 '05 #2
On Fri, 11 Jun 2004 03:51:01 -0700, Duncan Allen <Duncan Al***@discussions.microsoft.com> wrote:

¤ I've been using excel within VB.NET applications and I can't get it to close down and remove itself from memory.
¤
¤ I'm using Visual Studio 2003 ver 7.1.3088, Framework 1.1, MS Office 2003 with MS VS Tools for MS Office System 69586-3
¤
¤ After closing all workbooks etc and setting them to nothing, I run the following code to get rid of the Excel Application object
¤
¤ If Not (xlApp Is Nothing) Then
¤ xlApp.Quit()
¤ xlApp = Nothing
¤ GC.Collect()
¤ End If
¤
¤ This does set the application object to nothing in the locals panel but the Excel.EXE process is still running in the Task Manager.

See if the following helps:

PRB: Office Application Does Not Quit After Automation from Visual Studio .NET Client
http://support.microsoft.com/default...b;EN-US;317109
Paul ~~~ pc******@ameritech.net
Microsoft MVP (Visual Basic)
Nov 22 '05 #3

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

Similar topics

2
by: Duncan Allen | last post by:
I've been using excel within VB.NET applications and I can't get it to close down and remove itself from memory. I'm using Visual Studio 2003 ver 7.1.3088, Framework 1.1, MS Office 2003 with MS VS...
11
by: Mr. Smith | last post by:
Hello all, My code can successfully open, write to, format and save several worksheets in a workbook then save it by a given name, close and quit excel. My problem is that if I try and do it...
3
by: user_5701 | last post by:
Hello, I have an Access 2000 database that I need to export certain queries to Excel 2000. The problem is that I have to take the toolbars away from the users for security purposes, but still let...
4
by: Lisa | last post by:
Hi - I'm able to open excel workbooks and word documents, but I can't seem to copy excel charts, named ranges, etc. to a word document. Anyone know of good reference material in this area? What...
5
by: Tim Frawley | last post by:
I created a .NET Com Class object for use in ASP reports to export database results directly to Excel. I have it all working just find but I cannot get the Excel process to go away after the job...
16
by: alexia.bee | last post by:
Hi all, In some weird reason, excel instance won;t die if i remove the comment from 4 lines of setting values into struct. here is a snipcode public...
9
by: Doug Glancy | last post by:
I got the following code from Francesco Balena's site, for disposing of Com objects: Sub SetNothing(Of T)(ByRef obj As T) ' Dispose of the object if possible If obj IsNot Nothing AndAlso...
2
by: Nicholas Dreyer | last post by:
The following error Run-time exception thrown : System.Runtime.InteropServices.COMException - Error loading type library/DLL. happens while running the code listed at the bottom of this...
4
by: John Brock | last post by:
I have a .NET application that, among other things, creates Excel workbooks, and I have run into a very strange problem involving formulas on one worksheet that reference values on another...
2
by: AlenaMezh | last post by:
Hello! My Code destroys Excel's normal work.After running this code I acan' open excel file as normal. Do you know why? Please, answer. Public Function import_test_1() Dim objXL As Object Dim...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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
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.