0

I want to quit Excel, which is opening certain files when there are a lot of excel running.

I know how to run Excel excel and know how to terminate the process, but I do not know how to exit excel that reads certain files.

I wonder if there is a way to shut down excel (or office) running a particular file through an interop or some other way.

Ratell
  • 1
  • Possible duplicate of [Get the current Workbook Object in C#](https://stackoverflow.com/questions/7916711/get-the-current-workbook-object-in-c-sharp) – xdtTransform May 10 '19 at 06:35
  • https://social.msdn.microsoft.com/Forums/office/en-US/060000d8-a899-49bf-a965-0576dee958d4/how-to-get-active-application?forum=exceldev . – xdtTransform May 10 '19 at 06:40
  • for many excell process this one give the guidlines on how to loop throught all process. If you have issue with this solution please [edit] and [mcve] in your question – xdtTransform May 10 '19 at 06:41

3 Answers3

0

Excel is a red herring here, what you're actually looking to do is discover which running process has locked a particular file on disk. It doesn't matter that it's an excel file in particular. This is a fairly common requirement and is well covered in other SO answers such as:

How do I find out which process is locking a file using .NET?

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
0

This is what you want:

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

To close a specific/particular excel file

private static void KillSpecificExcelFileProcess(string excelFileName)
{
    var processes = from p in Process.GetProcessesByName("EXCEL")
                    select p;

    foreach (var process in processes)
    {
        if (process.MainWindowTitle == excelFileName.Substring(0, excelFileName.IndexOf(".")) + " - Excel" )
            process.Kill();
    }
}

Pass the File name in the method call and in result the specific process will be close/kill

Amit
  • 34
  • 1
  • 6