Excel instance won't unload from memory

S

Scott B

I've read another post where it says to make sure you don't use unqualified
references like ActiveWorkbooks, and to make sure you set all your references
to null when Quitting Excel. I wrote a very simple C# app (below) where I am
doing these things, but the EXCEL instance can still be seen under Processes
until I close the Windows App.
In my real app, the user can be running a function a number of times, and
each one launches it's own Excel instance, so we end up with a bunch of these
in memory until the main app is closed.
We have an old VB6 app using the Excel 5 library, and it doesn't have this
problem.
Any other pointers?

-----
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TestWriteExcel2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Excel.Application oExcel = new Excel.Application();
oExcel.Visible = true;

try
{
Excel.Workbook obWb =
oExcel.Workbooks.Open("c:\\temp\\testWrite.xls",

System.Type.Missing,
false,
System.Type.Missing,
System.Type.Missing,
System.Type.Missing,
false,
System.Type.Missing,
System.Type.Missing,
System.Type.Missing,
System.Type.Missing,
System.Type.Missing,
System.Type.Missing,
System.Type.Missing,
System.Type.Missing);


Excel.Worksheet wks = (Excel.Worksheet)obWb.Worksheets[1];
Excel.Range rng = wks.get_Range("C12", System.Type.Missing);
rng.Value2 = this.textBox1.Text;
rng = null;
wks = null;
//obWb.Save();
obWb.Close(true, System.Type.Missing, System.Type.Missing);
obWb = null;
oExcel.Quit();
oExcel = null;
}
catch (Exception ex)
{

MessageBox.Show("Error opening Excel file.\n" + ex.Message);

}
}
}
}
 
Z

zz

i do not no about c#, but in vba to terminate the excel entitie you need to
do "Application.Terminate" and set the oExcel object explicitally to
"nothing"
 
S

Scott B

Ho ho! That seemed to do it, at least in my simple test app. Now let's see
if I can apply it to my main app. Thanks.

The answer to to add GC.Collec();
--
Scott B


Tom Ogilvy said:
I don't know anything about .NET, but note that at the end of this sample,
they do garbage collection:

http://support.microsoft.com/kb/303872/en-us

Which corresponds to what I recall is a necessary step.

--
Regards,
Tom Ogilvy


Scott B said:
I've read another post where it says to make sure you don't use unqualified
references like ActiveWorkbooks, and to make sure you set all your references
to null when Quitting Excel. I wrote a very simple C# app (below) where I am
doing these things, but the EXCEL instance can still be seen under Processes
until I close the Windows App.
In my real app, the user can be running a function a number of times, and
each one launches it's own Excel instance, so we end up with a bunch of these
in memory until the main app is closed.
We have an old VB6 app using the Excel 5 library, and it doesn't have this
problem.
Any other pointers?

-----
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TestWriteExcel2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Excel.Application oExcel = new Excel.Application();
oExcel.Visible = true;

try
{
Excel.Workbook obWb =
oExcel.Workbooks.Open("c:\\temp\\testWrite.xls",

System.Type.Missing,
false,
System.Type.Missing,
System.Type.Missing,
System.Type.Missing,
false,
System.Type.Missing,
System.Type.Missing,
System.Type.Missing,
System.Type.Missing,
System.Type.Missing,
System.Type.Missing,
System.Type.Missing,
System.Type.Missing);


Excel.Worksheet wks = (Excel.Worksheet)obWb.Worksheets[1];
Excel.Range rng = wks.get_Range("C12", System.Type.Missing);
rng.Value2 = this.textBox1.Text;
rng = null;
wks = null;
//obWb.Save();
obWb.Close(true, System.Type.Missing, System.Type.Missing);
obWb = null;
oExcel.Quit();
oExcel = null;
}
catch (Exception ex)
{

MessageBox.Show("Error opening Excel file.\n" + ex.Message);

}
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top