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