Radu Adrian Ciora schrieb:
> Hi, this is the sencond time I try to post to this mailing list but I
> do this because I don't think I succeded the first time, as I didn't
> receive my message.(Sorry for the duplication if any.) I've tried to
> modify the unittest that tests the ShowEvents method like this:
> 
>     def test(self):
>         # Start IE, call .Quit(), and check if the
>         # DWebBrowserEvents2_OnQuit event has fired.  We do this by
>         # calling ShowEvents() and capturing sys.stdout.
>         #o = comtypes.client.CreateObject("InternetExplorer.Application")
>         o = comtypes.client.CreateObject("Word.Application")
>         conn = comtypes.client.ShowEvents(o)
>         o.Quit()
>         del o
> 
>         self.pump_messages_with_timeout(1000)
> 
>         stream = sys.stdout
>         stream.flush()
>         sys.stdout = self.old_stdout
>         output = stream.getvalue().splitlines()
>         print "output is: "
>         print output
> 
>         #self.failUnless('# event found: DWebBrowserEvents2_OnMenuBar' in 
> output)
>         #self.failUnless('Event DWebBrowserEvents2_OnQuit(None)' in output)
> 
> 
> 
> When I put "Word.Application" insead of
> "InternetExplorer.Application", I don't get to see any events caught
> by the ShowEvents method.

I think there are no events fired in the code you execute above.
The following code works for me in the Windows command prompt:

c:\>python
Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on 
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys; sys.coinit_flags = 0
>>> from comtypes.client import CreateObject, ShowEvents
>>> word = CreateObject("Word.Application")
>>> conn = ShowEvents(word)
# event found: ApplicationEvents_Startup
# event found: ApplicationEvents_Quit
# event found: ApplicationEvents_DocumentChange
>>> word.Visible = True
>>> Event ApplicationEvents_DocumentChange(None)
Event ApplicationEvents_Quit(None)

>>>

A few comments about this code:

1. 'word.Visible = True' displays the Word window on the screen.  Then I 
created a new document
by selecting 'New' from the menu in Word, this led to the 'Event 
ApplicationEvents_DocumentChange(None)'.
Finally I finished Word, again by using the menu in Word, which led to the 
second event.

2. 'import sys; sys.coinit_flags = 0' initializes COM for a multithreaded 
appartment.
Must be executed before importing comtypes; comtypes looks for this attribute 
to determine
how COM should be initialized.
A mutilthreaded appartment is very useful if you want to work with COM in a 
console program where
the main thread does not have a message loop; it avoids that you have to create 
a messageloop yourself.


If you execute the code in PythonWin, for example, you should NOT set 
sys.coinit_flags to zero.
This is for two reasons:  First, Pythonwin has already initialized COM, and it 
cannot be initialized
a second time in a different threading mode - you will get an exception if you 
try this.
Second, it is not even needed to create a messageloop since PythonWin - as a 
GUI application -
already has a messageloop.

Note that all this isn't even comtypes specific - win32com behaves in exactly 
the same way.

Hope this helps,

Thomas


-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >> http://get.splunk.com/
_______________________________________________
comtypes-users mailing list
comtypes-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/comtypes-users

Reply via email to