explicitly close an application in windows

Mark Hammond MarkH at ActiveState.com
Thu Jun 21 04:38:44 EDT 2001


Christian Reyes wrote:

> Is there a method that one can use to explicitly close an application in
> windows?
> 

The following script will close Microsoft Word, and _may_ work for other 
apps :)

Of course, if you really want to close MSWord, then COM it a better 
solution, but I assume the app you want to close does not support COM.

Further, this will only close Word if Word would itself close manually - 
ie, Word will still prompt to save changed files, etc.

Anyway, for what it is worth...

import win32con, win32gui

def Callback(hwnd, ignored):
     if win32gui.GetWindowText(hwnd).find("Microsoft Word")>=0:
         print "Closing hwnd", hwnd
         win32gui.PostMessage( hwnd, win32con.WM_CLOSE,0,0)

win32gui.EnumWindows(Callback, None)

Mark.




More information about the Python-list mailing list