1

I have a job server that automates various reports during the day in Excel (using MS Query). Invariably, one or more reports will abend, leaving an instance of Excel open that has to be purged.

The cleanup job uses Interop and looks like this:

Excel.Application xl;

for (int i = 0; i < 20; i++)
{
    try
    {
        xl = (Excelx.Application)Marshal.GetActiveObject("Excel.Application");
        xl.Visible = true;
        xl.Quit();
    }
    catch (Exception ex)
    {
        break;
    }
}

It works most of the time, but every once in a while I'll get an instance of Excel that doesn't want to die. I can manually kill the process in the task manager the next morning, but I haven't found a way to replicate that in my cleanup job that runs the night before.

If anyone can point me in the direction of how I can add a follow-up task to the job above that's slightly more "rude" and will kill anything named EXCEL.exe, It'd appreciate the hint.

Also, I have to run this job for every user on the machine. If there is a way around that, I'd welcome that feedback as well.

Hambone
  • 15,600
  • 8
  • 46
  • 69

1 Answers1

0

It appears that brute-force closing the processes is the way to go:

foreach (var process in Process.GetProcessesByName("excel"))
{
    process.Kill();
}

https://stackoverflow.com/a/3345421/1278553

Community
  • 1
  • 1
Hambone
  • 15,600
  • 8
  • 46
  • 69