Craig Murphy: author, blogger, community evangelist, developer, speaker

The Social Programmer

June 29th, 2005 at 1:52 am

Excel Interop – killing Excel.exe

in: .net

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

This posting over at ScottishDevelopers got me thinking, especially since I’ve used Excel and Word from a number of Delphi applications over the last eight years. I have come across this problem in the past (Win32), so I thought it should be fairly easy to chase.

After a bit of research, The Code Project suggested that the Excel process be killed off through code:

Process[] pProcess; 
pProcess = System.Diagnostics.Process.GetProcessesByName("Excel");
pProcess[0].Kill();

Well, that would work, but it might kill off the wrong instance, or kill off the user’s precious ‘expenses’ spreadsheet instead of the one we created.

To help me trace the problem, I used the following code:

using System.Diagnostics;
using System.Runtime.InteropServices;

Excel.Application	excelApp = null;
Excel.Workbook	excelWorkbook = null;
Excel.Sheets	excelSheets = null;
Excel.Worksheet	excelWorksheet = null;
Excel.Workbooks	excelWorkbooks = null;

private void button3_Click(object sender, System.EventArgs e)
{
  excelApp                 = new Excel.ApplicationClass();
  excelApp.Visible  = true;
  excelWorkbooks   = excelApp.Workbooks;
  excelWorkbook = excelWorkbooks.Add(System.Reflection.Missing.Value);
  excelSheets = excelWorkbook.Worksheets;
  excelWorksheet = (Excel.Worksheet)   excelSheets.get_Item(1);
  excelWorksheet.Cells[1,1] = "42";
}

private void button4_Click(object sender, System.EventArgs e)
{
  excelWorkbook.Close (false, System.Reflection.Missing.Value,System.Reflection.Missing.Value) ; 

  excelWorkbooks.Close();
  excelApp.Quit();

  Marshal.ReleaseComObject(excelWorksheet);
  Marshal.ReleaseComObject(excelSheets);
  Marshal.ReleaseComObject(excelWorkbooks);
  Marshal.ReleaseComObject(excelWorkbook);
  Marshal.ReleaseComObject(excelApp);

  excelWorksheet = null;
  excelSheets = null;
  excelWorkbooks = null;
  excelWorkbook = null;
  excelApp = null;

  GC.GetTotalMemory(false);
  GC.Collect();
  GC.WaitForPendingFinalizers();
  GC.Collect();
  GC.GetTotalMemory(true);
}

This code creates an Excel application, populates it, then lets us close it. Invoking the button3 code creates it, button4 removes it.

Now, without the calls to the garbage collector, excel.exe hung around if the code behind button3, button4, button3, button4 is invoked.

In the short-term, I would suggest adding the additional calls to the garbage collector (although, and this confirms it, memory reclamation is not guaranteed). It’s time to read up on the garbage collector and COM Interop.

I used Visual Studio 2003 and Office 2003 to test this.

Technorati Tags: , , ,

Tags: , , ,
-