[python-win32] launching Word with COM..how to tell when it's closed?

Jens B. Jorgensen jens.jorgensen@tallan.com
Thu, 04 Apr 2002 09:28:33 -0600


I'm going to say a good way to go (and one of few ways to go, to be 
sure) will be to get the process ID of the Word.exe process, open a 
handle on that process with syncronize permissions,  and just 
WaitForSingleObject(...) on the handle. Just waiting on the handle 
should mean that the message loop cannot be run and thus you app will be 
hung until it returns. You app being hung may be a bit much in terms of 
not letting the user touch your app until Word exits since the app won't 
even repaint itself then and users might freak out. Unless there's some 
method in the Word app object that'll get you the process Id I'm 
guessing the only thing you can do to get the process id is to get a 
list of processes and just pick the first one called Word. The way to 
get a list of processes is to use the windows performance monitoring 
API. I've got a PerfData module that processes performance data blocks 
and a pyRegistry module that can properly get the performance data using 
the Registry interface. Here's an example I've cut out of an app I use:

# query performance data to get the Process Id of
# the CQMULATE process
perf_data = PerfData.query('Process')
pobj = perf_data.getObjectByName('Process')
# look for the CQMULATE process
try :
    ids = pobj.getCounterValue('CQMULATE', 'ID Process')
except ValueError, e :
    try :
        s.send('CQMULATE process not running, will sleep for 10 
seconds\r\n')
        if WaitForSingleObject(self.hWaitStop, 10*1000) == WAIT_OBJECT_0 :
            s.send('*** service shutting down***\r\n')
            s.close()
            return
    except socket.error, se :
        client_connected = 0
    continue

# now we have to open the process so we can wait on the
# handle
proch = OpenProcess(win32con.SYNCHRONIZE, 0, ids[0])

You can pick up my PerfData.py module from 
http://www.ultraemail.net/~jbj1/PerfData.py and find my pyRegistry 
module which you'll also need on http://www.ultraemail.net/~jbj1

David Primmer wrote:

>I have a wxWindows application that is starting a Word session from a
>button click with self.w = win32com.client.Dispatch("Word.Application")
>and I'd like to have Word be "modal" so that the user cannot do anything
>in my app while Word is open (I'm editing data with it). Is there any
>event that Word would trigger that I could capture to know when the user
>has saved and closed Word? 
>
>I can test for the presence of the Word object within my program but I'm
>guessing that would require some kind of polling mechanism (I'm not sure
>how to do that either). 
>
>Would I have to make my app a COM server and then send a message from
>Word when it closes? Don't really want to mess with something like that.
>
>Any ideas are appreciated. Thanks.
>
>davep
>
>
>
>_______________________________________________
>Python-win32 mailing list
>Python-win32@python.org
>http://mail.python.org/mailman/listinfo/python-win32
>


-- 
Jens B. Jorgensen
jens.jorgensen@tallan.com