0

I want to be able to end a task called "Microsoft Excel - test.xlsx" without ending any other applications that are also Excel applications.

I have used

foreach (Process process in Process.GetProcesses())
            {
                if (process.ProcessName.Equals("EXCEL"))
                    process.Kill();
            }

but this kills all other Excel workbooks that are open. Is it possible to only kill or end the specified workbook?

javacoder123
  • 193
  • 3
  • 17
  • 1
    Yes, Excel runs multiple top-level windows from a single process. If you want to close a particular spreadsheet, you don't do it by killing the process. – Damien_The_Unbeliever Nov 22 '18 at 09:30
  • I did suspect as much, is there a way to end the application in C#? – javacoder123 Nov 22 '18 at 09:34
  • @javacoder123 refer this [answer](https://stackoverflow.com/a/33951116/5002329) – Sushil Jadhav Nov 22 '18 at 09:58
  • Possible duplicate of [C# - Kill EXCEL.exe process referencing a particular file](https://stackoverflow.com/questions/33950988/c-sharp-kill-excel-exe-process-referencing-a-particular-file) – Sani Huttunen Nov 22 '18 at 10:15
  • That is still kill the process not ending the individual task. By using the accepted solution it kills the process. There is only one process for excel in the task manager and multiple workbooks open. I want to stop the application not the entire process. – javacoder123 Nov 22 '18 at 10:16
  • @SaniSinghHuttunen this is not a duplicate as the other solution ends the process not the application itself. The accepted answer says "it will also close all opened files with the same name". Therefore, it closes all applications that are of an excel format not the individual application. – javacoder123 Nov 22 '18 at 10:18
  • @SaniSinghHuttunen No the other question kills the process my question is how to end a task. Excel has one process even though i've got 50 workbooks open. If you kill the process like the other question asks and all the answers that where given then you would be closing all 50 workbooks. My question is how can I close only one of the workbooks. Please read the answer to the other question carefully as they are not helpful or relevant to this situation. – javacoder123 Nov 22 '18 at 13:21

1 Answers1

1

My solution finds the workbook and then closes it.

/// <summary>
    /// Gets the current Excel process and specified workbook and closes it.
    /// If the workbook was the only one in the application, it then closes excel as well.
    /// </summary>
    public static void CloseWorkbook()
    {
        try
        {
            Application excelApplicationProcess = (Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application"); // get current excel process

            foreach (object wb in excelApplicationProcess.Workbooks)
            {
                if (((Workbook)wb).FullName.Equals(Settings.Default.Test))
                    ((Workbook)wb).Close(false, Settings.Default.Test, Missing.Value);
            }

            if (excelApplicationProcess.Workbooks.Count == 0) // if it was the only one close excel as well
                excelApplicationProcess.Quit();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
javacoder123
  • 193
  • 3
  • 17