Avatar of dma70
dma70Flag for United States of America asked on

Killing specific processes - I have multiple EXCEL spreadsheets running in real time. They all need to be closed once a day automatically. Is there a way to tag each process?

I have multiple versions of excel running real time applications.   The programs have to be stopped and restarted during off-hours.   Some processes need to be stopped at one time and others at another time.   The processes are on several users machines.   Is there a way to identify a process so a script can be written to save and close excel for later restart.   My IT guy says he can't tell one EXCEL process from another - so he cant target which one to close down.
Windows 7

Avatar of undefined
Last Comment

8/22/2022 - Mon
Kyle Abrahams

Yes, this can be done via excel VBA.


I would create a generic workbook on each computer.  Then exec the workbook with the auto_open macro to close needed workbooks, saving as needed.

Not trying to determine if workbook is open.  It will be.  Trying to figure out how to identify which excel process is which.  Assume there are 10 of them and I want to shut down only one.  How does the OS script identify which process to close?
Frank White

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

Using C# you can loop through the processes using System.Diagnostic. You can use the "MainWindowTitle" property to determin the name of the file currently opened by that process... Here is a short snippet that loops through the Excel processes:
foreach (Process p in Process.GetProcesses())
                if (p.ProcessName == "EXCEL")


Open in new window

once you have the right process, you can use the p.Kill method to terminate the process.

Note: You could also use Office Interop to attach to the Excel Process, and save any updates and close properly instead of a Kill method. refer to MSDN OfficeInterop.Excel for more details.
Your help has saved me hundreds of hours of internet surfing.
Kyle Abrahams

"Not trying to determine if workbook is open.  It will be."

Never assume that the program will work exactly as you intend it to.  Most errors in programs are caused by unforseen circumstances.  For instance, a user logs out their machine but leaves it on.  You call the script against that computer to close the workbook and viola, the code just broke.  Always better to be safe than sorry.

As DaFranker pointed out (thanks for the verbose explination . . . that was exactly on target), the referenced code was to find the workbook if it's open, and from there you can close the workbook forcing the save

CloudedTurtle's system.Reflection method is an elegant solution that can also accomplish the same task.  

Regardless there are multiple ways of achieving your ends.

My IT guy doesn't know c# - but I think these answers are going in the right direction.  Being able to drill down and get the name of the spreadsheet sounds like a viable solution if it were easy to program.   Seems like the best solution is to put a clock into vb code and when the right time comes shut the program down.

Handle the timing using the Windows Scheduler, this is much easier, and less problematic than having an application that is always running (less chance of memory leaks).

PS: There are equivalent methods that I showed in C# available for VB.Net as well. i just don't know them. :-)
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.

thank you  -