Not another window to click…

This is a very short, but hopefully practical, post that stands as much an aide memoire for me as of benefit for you. Doing tasks that are repetitive or routine should be handled by the computer as far as it possible. Why waste time and effort when the computer can perform a task billions of times faster than we can, allowing us the opportunity to get on with something more useful.

Today I looking at automating a process involving Excel; some legacy code was rather sloppy at managing the instances of Excel it spawned and I ended up with a scrawling mess of Microsoft’s finest slowing my machine and draining memory which was becoming increasingly frustrating.

What should happen

At the code level, garbage collectors runs to sweep up any objects that are no longer required, but there’s nothing suitable at the OS level for us to tap into. We could look at using the Process ID perhaps and scripting something but that would be overkill for simple or one-off tasks. If all we want to do is to close all instances of a program then we can use the ‘taskkill’ command in Windows

There’s as a good article talking about the extra polish we can put on TaskKill so I won’t repeat them here but I suggest you check it out if you want to customise your code.

A note of caution should be taken than executing this code will shut all copies down without prompting to save.


Copy the following into a text editor and then save as YourFileName.bat or alternatively download the zip file from the link below

Windows Batch File:

[sourcecode language=”vb”]
taskkill /f /im Excel.exe


[sourcecode language=”python”]
import os

os.system(‘taskkill /f /im Excel.exe’)


Batch file to close all open Excel workbooks

I'd love to hear what your thoughts are...please feel free to leave a reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.