汪雷 wrote: > Hi, I don't know how to handle ms word's events. > By VBA, the code is as following: > > > Private Sub Ducoment_Open(): > > my code > > End Sub. > > The code in ms word macro is to capture the 'Open' event, when the word > is opened, my code will be executed. How can I perform by python-win32.
First, I want to make sure you understand the limitations. If your Python code has launched the Word.Application as a COM server, then you can certainly handle events for that instance. But you cannot handle all Word events everywhere. For example, if some user just starts Word and opens a Word document, you can't catch that, because your Python process is not running. So, with that, here's an example that handles the DocumentOpen event from Word. Note that you'll be handling events from the Word Application object ("DocumentOpen" is an Application event). Your VBA code above is handling the "Open" event on the "Document" object. That's harder to do on Python. import win32com.client class myHandler(object): def __init__(self): print "__init__" def OnDocumentOpen(self,doc): print "Got document!" print doc ob = win32com.client.DispatchWithEvents("Word.Application", myHandler) ob.Visible = True ob.Documents.Open("c:/tmp/xxx.doc") ob.Application.Quit() -- Tim Roberts, t...@probo.com Providenza & Boekelheide, Inc. _______________________________________________ python-win32 mailing list python-win32@python.org http://mail.python.org/mailman/listinfo/python-win32