[python-win32] Passing Nothing into ReferencesEvents - resolved, however can't bind events

shivisi shivisi at nana10.co.il
Thu Oct 15 13:50:45 CEST 2009


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)
#-----------------------------------------------------------------------------
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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/ms-tnef
Size: 5288 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-win32/attachments/20091015/86474844/attachment.bin>


More information about the python-win32 mailing list