<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Generator" CONTENT="MS Exchange Server version 6.5.7651.59">
<TITLE>[python-win32] Passing Nothing into ReferencesEvents - resolved, however can't bind events</TITLE>
</HEAD>
<BODY>
<!-- Converted from text/plain format -->

<P><FONT SIZE=2>Thanks for taking the time to answer, and for pywin. I've been using pywin with COM for some time, and am only now (when something doesn't &quot;just work&quot;) starting to appreciate its internal complexities.<BR>
<BR>
I get back the following error:<BR>
&gt;&gt;&gt; com_error: (-2147467262, 'No such interface supported', None, None)<BR>
I have a very sketchy understanding of COM's internals, but if the IDispatch interface was supported, wouldn't Dispatch() be using that?<BR>
Also, is there any way of getting the interfaces which an object does support?<BR>
<BR>
Thanks again,<BR>
Zev<BR>
<BR>
-----Original Message-----<BR>
From: Mark Hammond [<A HREF="mailto:mhammond@skippinet.com.au">mailto:mhammond@skippinet.com.au</A>]<BR>
Sent: Sat 10/17/2009 00:38<BR>
To: shivisi<BR>
Cc: python-win@python.org<BR>
Subject: Re: [python-win32] Passing Nothing into ReferencesEvents - resolved, however can't bind events<BR>
<BR>
Sorry, I did mislead you.&nbsp; After reading this in more detail your<BR>
initial code looked correct.<BR>
<BR>
Try something like:<BR>
<BR>
disp =<BR>
acc.VBE.Events.ReferencesEvents(None).QueryInterface(pythoncom.IID_IDispatch)<BR>
refsEvents=DispatchWithEvents(disp,RefEvents)<BR>
<BR>
To ensure we are passing an IDispatch rather than an IUnknown to<BR>
DispatchWithEvents.<BR>
<BR>
HTH,<BR>
<BR>
Mark<BR>
<BR>
<BR>
<BR>
On 16/10/2009 7:32 PM, shivisi wrote:<BR>
&gt; I'm confused.<BR>
&gt;<BR>
&gt; DispatchWithEvents fails on Access.Application, because there are no events on that object.<BR>
&gt; The events of interest are on the associated VB Editor/VB Project, via the VBA Extensibility Library. VBE is the root object of that library, and that is the type of acc.VBE.<BR>
&gt; These events are retrieved with the whole path:<BR>
&gt;&gt;&gt;&gt; acc.VBE.Events.ReferencesEvents(None)<BR>
&gt; where acc is the Access.Application object.<BR>
&gt;<BR>
&gt; If the Access.Application did have events, would win32com.PumpMessages() return events for all the child objects?<BR>
&gt; If it does, then perhaps I can use:<BR>
&gt;&gt;&gt;&gt; DispatchWithEvents(acc.VBE.Events,RefEvents)<BR>
&gt;&gt;&gt;&gt; win32com.PumpMessages()<BR>
&gt; However, ReferencesEvents is a &quot;property with arguments&quot;, and needs to be passed None.<BR>
&gt;<BR>
&gt; It seems that ReferencesEvents(None) returns a PyIUnknown object, and DispatchWithEvents can only bind to (at a minimum) PyIDispatch. Would comtypes help me here?<BR>
&gt;<BR>
&gt; -----Original Message-----<BR>
&gt; From: Mark Hammond [<A HREF="mailto:skippy.hammond@gmail.com">mailto:skippy.hammond@gmail.com</A>]<BR>
&gt; Sent: Fri 10/16/2009 02:00<BR>
&gt; To: shivisi<BR>
&gt; Cc: python-win32@python.org<BR>
&gt; Subject: Re: [python-win32] Passing Nothing into ReferencesEvents - resolved, however can't bind events<BR>
&gt;<BR>
&gt; On 15/10/2009 10:50 PM, shivisi wrote:<BR>
&gt;&gt; Passing Application.VBE.Events.ReferencesEvents(None) works (at least at this point).<BR>
&gt;&gt; However, when I try to bind to an event class, I get the error below.<BR>
&gt;&gt; Any assistance, debugging suggestions, would be greatly appreciated.<BR>
&gt;&gt; Thanks.<BR>
&gt;&gt;<BR>
&gt;&gt; # Code<BR>
&gt;&gt; from win32com.client import Dispatch, DispatchWithEvents, getevents<BR>
&gt;&gt;<BR>
&gt;&gt; class RefEvents:<BR>
&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; def OnItemAdded(self,ref):<BR>
&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print '--Event - Reference added'<BR>
&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; def OnItemRemoved(self,ref):<BR>
&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print '--Event - Reference removed'<BR>
&gt;&gt;<BR>
&gt;&gt; acc=Dispatch('Access.Application')<BR>
&gt;&gt; acc.Visible=True<BR>
&gt;&gt; refsEvents=DispatchWithEvents(acc.VBE.Events.ReferencesEvents(None),RefEvents)<BR>
&gt;<BR>
&gt;<BR>
&gt; See the docstring for DispatchWithEvents - you probably want something<BR>
&gt; closer to:<BR>
&gt;<BR>
&gt; acc = DispatchWithEvents('Access.Application', RefEvents)<BR>
&gt; acc.Visible = True<BR>
&gt; win32com.PumpMessages() # sit here doing nothing but waiting for events...<BR>
&gt;<BR>
&gt; Cheers,<BR>
&gt;<BR>
&gt; Mark<BR>
&gt;<BR>
&gt;<BR>
&gt;&gt; #-----------------------------------------------------------------------------<BR>
&gt;&gt; Traceback (most recent call last):<BR>
&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; File &quot;&lt;interactive input&gt;&quot;, line 1, in&lt;module&gt;<BR>
&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; File &quot;C:\Python26\lib\site-packages\win32com\client\__init__.py&quot;, line 245, in DispatchWithEvents<BR>
&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; disp = Dispatch(clsid)<BR>
&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; File &quot;C:\Python26\lib\site-packages\win32com\client\__init__.py&quot;, line 96, in Dispatch<BR>
&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return __WrapDispatch(dispatch, userName, resultCLSID, typeinfo, clsctx=clsctx)<BR>
&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; File &quot;C:\Python26\lib\site-packages\win32com\client\__init__.py&quot;, line 43, in __WrapDispatch<BR>
&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return dynamic.Dispatch(dispatch, userName, WrapperClass, typeinfo, clsctx=clsctx)<BR>
&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; File &quot;C:\Python26\lib\site-packages\win32com\client\dynamic.py&quot;, line 118, in Dispatch<BR>
&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; typeinfo = IDispatch.GetTypeInfo()<BR>
&gt;&gt; AttributeError: 'PyIUnknown' object has no attribute 'GetTypeInfo'<BR>
&gt;&gt; #-----------------------------------------------------------------------------<BR>
&gt;&gt;&gt;&gt;&gt; acc.VBE.Events.ReferencesEvents<BR>
&gt;&gt; &lt;bound method Events.ReferencesEvents of&lt;win32com.gen_py.Microsoft Visual Basic for Applications Extensibility 5.3.Events instance at 0x17466752&gt;&gt;<BR>
&gt;&gt;&gt;&gt;&gt; acc.VBE.Events.ReferencesEvents(None)<BR>
&gt;&gt; &lt;PyIUnknown at 0x0102CFF8 with obj at 0x00190AEC&gt;<BR>
&gt;&gt;<BR>
&gt;&gt;<BR>
&gt;&gt; Original message:<BR>
&gt;&gt;&gt; I am writing a COM addin, and attempting to trap the ItemAdded/ItemRemoved event in the following object:<BR>
&gt;&gt;&gt; Application.VBE.Events.ReferencesEvents<BR>
&gt;&gt;&gt; which is retrieved by passing Nothing as a paramter:<BR>
&gt;&gt;&gt; Application.VBE.Events.ReferencesEvents(Nothing)<BR>
&gt;&gt;&gt;<BR>
&gt;&gt;&gt; Tim Golden wrote in the following post (<A HREF="http://mail.python.org/pipermail/python-win32/2009-February/008826.html">http://mail.python.org/pipermail/python-win32/2009-February/008826.html</A>) that pythoncom.Empty can be used for Nothing. However, when trying this:<BR>
&gt;&gt;&gt;<BR>
&gt;&gt;&gt; try:<BR>
&gt;&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.comRefevents=self.app.VBE.Events.ReferencesEvents(pythoncom.Empty)<BR>
&gt;&gt;&gt; except pythoncom.com_error, (hr, msg, exc, arg):<BR>
&gt;&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print &quot;The call failed with code %d: %s&quot; % (hr, msg)<BR>
&gt;&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if exc is None:<BR>
&gt;&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print &quot;There is no extended error information&quot;<BR>
&gt;&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else:<BR>
&gt;&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; wcode, source, text, helpFile, helpId, scode = exc<BR>
&gt;&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print &quot;The source of the error is&quot;, source<BR>
&gt;&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print &quot;The error message is&quot;, text<BR>
&gt;&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print &quot;More info can be found in %s (id=%d)&quot; % (helpFile, helpId)<BR>
&gt;&gt;&gt;<BR>
&gt;&gt;&gt; I get back the following (the same as if I had passed no arguments at all):<BR>
&gt;&gt;&gt;<BR>
&gt;&gt;&gt; The call failed with code -2147352561: Parameter not optional.<BR>
&gt;&gt;&gt; There is no extended error information<BR>
&gt;&gt;&gt;<BR>
&gt;&gt;&gt; Eric Lippert states in his blog (http://blogs.msdn.com/ericlippert/archive/2004/07/14/183241.aspx):<BR>
&gt;&gt;&gt;&gt; A common alternative to passing a missing argument is to pass Nothing, Null, or Empty in VBScript,<BR>
&gt;&gt;&gt;&gt; null or undefined in JScript.&nbsp; Null and null pass VT_NULL, Empty and undefined pass VT_EMPTY, and<BR>
&gt;&gt;&gt;&gt; Nothing passes a VT_DISPATCH with no value dispatch object pointer.<BR>
&gt;&gt;&gt; It would seem that VB's Nothing and Empty are two different things.<BR>
&gt;&gt;&gt;<BR>
&gt;&gt;&gt; If I try passing pythoncom.VT_DISPATCH, then I get the following traceback:<BR>
&gt;&gt;&gt;<BR>
&gt;&gt;&gt; -----------------------------------------------------------------------------------------------------------<BR>
&gt;&gt;&gt; Traceback (most recent call last):<BR>
&gt;&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; File &quot;C:\Python26\lib\site-packages\win32com\universal.py&quot;, line 177, in dispatch<BR>
&gt;&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; retVal = ob._InvokeEx_(meth.dispid, 0, meth.invkind, args, None, None)<BR>
&gt;&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; File &quot;C:\Python26\lib\site-packages\win32com\server\policy.py&quot;, line 324, in _InvokeEx_<BR>
&gt;&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return self._invokeex_(dispid, lcid, wFlags, args, kwargs, serviceProvider)<BR>
&gt;&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; File &quot;C:\Python26\lib\site-packages\win32com\server\policy.py&quot;, line 649, in _invokeex_<BR>
&gt;&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return DesignatedWrapPolicy._invokeex_( self, dispid, lcid, wFlags, args, kwArgs, serviceProvider)<BR>
&gt;&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; File &quot;C:\Python26\lib\site-packages\win32com\server\policy.py&quot;, line 585, in _invokeex_<BR>
&gt;&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return func(*args)<BR>
&gt;&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; File &quot;C:\Documents and Settings\ABG7\My Documents\Zevi\Python-COMAddin\VBACodeRepository.py&quot;, line 104, in OnConnection<BR>
&gt;&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.comRefevents=self.app.VBE.Events.ReferencesEvents(pythoncom.VT_DISPATCH)<BR>
&gt;&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; File &quot;C:\Python26\lib\site-packages\win32com\gen_py\0002E157-0000-0000-C000-000000000046x0x5x3.py&quot;, line 145, in ReferencesEvents<BR>
&gt;&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ret = self._oleobj_.InvokeTypes(202, LCID, 2, (13, 0), ((13, 1),),VBProject<BR>
&gt;&gt;&gt; TypeError: The Python instance can not be converted to a COM object<BR>
&gt;&gt;&gt; pythoncom error: Unexpected gateway error<BR>
&gt;&gt;&gt;<BR>
&gt;&gt;&gt; Traceback (most recent call last):<BR>
&gt;&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; File &quot;C:\Python26\lib\site-packages\win32com\universal.py&quot;, line 177, in dispatch<BR>
&gt;&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; retVal = ob._InvokeEx_(meth.dispid, 0, meth.invkind, args, None, None)<BR>
&gt;&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; File &quot;C:\Python26\lib\site-packages\win32com\server\policy.py&quot;, line 324, in _InvokeEx_<BR>
&gt;&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return self._invokeex_(dispid, lcid, wFlags, args, kwargs, serviceProvider)<BR>
&gt;&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; File &quot;C:\Python26\lib\site-packages\win32com\server\policy.py&quot;, line 649, in _invokeex_<BR>
&gt;&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return DesignatedWrapPolicy._invokeex_( self, dispid, lcid, wFlags, args, kwArgs, serviceProvider)<BR>
&gt;&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; File &quot;C:\Python26\lib\site-packages\win32com\server\policy.py&quot;, line 585, in _invokeex_<BR>
&gt;&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return func(*args)<BR>
&gt;&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; File &quot;C:\Documents and Settings\ABG7\My Documents\Python-COMAddin\VBACodeRepository.py&quot;, line 104, in OnConnection<BR>
&gt;&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.comRefevents=self.app.VBE.Events.ReferencesEvents(pythoncom.VT_DISPATCH)<BR>
&gt;&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; File &quot;C:\Python26\lib\site-packages\win32com\gen_py\0002E157-0000-0000-C000-000000000046x0x5x3.py&quot;, line 145, in ReferencesEvents<BR>
&gt;&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ret = self._oleobj_.InvokeTypes(202, LCID, 2, (13, 0), ((13, 1),),VBProject<BR>
&gt;&gt;&gt; TypeError: The Python instance can not be converted to a COM object<BR>
&gt;&gt;&gt;<BR>
&gt;&gt;&gt;<BR>
&gt;&gt;&gt; _______________________________________________<BR>
&gt;&gt;&gt; python-win32 mailing list<BR>
&gt;&gt;&gt; python-win32@python.org<BR>
&gt;&gt;&gt; <A HREF="http://mail.python.org/mailman/listinfo/python-win32">http://mail.python.org/mailman/listinfo/python-win32</A><BR>
&gt;<BR>
&gt;<BR>
<BR>
<BR>
</FONT>
</P>

</BODY>
</HTML>