G
Guest
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);
}
}
}
}
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);
}
}
}
}