Zev Spitz schrieb: > I am coming from VBA, and my understanding of COM internals is very weak. > > 2 questions: > 1) What do I replace IWhatever with, and how do I find that out? (The > argument is supposed to be of type VBProject) > 2) How do I get an instance of IWhatever? >
>From your original post: > # 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) I don't have Access installed, but I assume Word is somewhat similar. So, here we go: Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> from comtypes.client import CreateObject, ShowEvents >>> acc = CreateObject("Word.Application") # Generating comtypes.gen._00020905_0000_0000_C000_000000000046_0_8_3 # Generating comtypes.gen._00020430_0000_0000_C000_000000000046_0_2_0 # Generating comtypes.gen.stdole # Generating comtypes.gen._2DF8D04C_5BFA_101B_BDE5_00AA0044DE52_0_2_3 # Generating comtypes.gen.Office # Generating comtypes.gen._0002E157_0000_0000_C000_000000000046_0_5_3 # Generating comtypes.gen._2DF8D04C_5BFA_101B_BDE5_00AA0044DE52_0_2_4 # Generating comtypes.gen.VBIDE # Generating comtypes.gen.Word >>> acc.Visible = True >>> acc.VBE.Events Traceback (most recent call last): File "<stdin>", line 1, in <module> _ctypes.COMError: (-2146822220, None, (u'Dem programmatischen Zugriff auf das Visual Basic-Projekt wird nicht vertraut.' , u'Microsoft Word', u'C:\\Programme\\Microsoft Office\\OFFICE11\\1031\\wdmain11.chm', 25548, None)) >>> (The error message means somewhat like 'The programmatic access to the visual basic project is not trusted. I use the 'Extras' menu in Word to change the macro security settings andtry again:) >>> acc.VBE.Events <POINTER(Events) ptr=0x2929bc at 1224170> >>> (Ah, some progress) >>> acc.VBE.Events.ReferencesEvents(None) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "c:\sf\comtypes\comtypes\__init__.py", line 795, in __call__ return self.getter(self.im_inst, *args) ctypes.ArgumentError: argument 1: <type 'exceptions.TypeError'>: None >>> Ok, None is not accepted. Grepping in the comtypes\gen directory for 'ReferencesEvents' I find this code snippet in file '_0002E157_0000_0000_C000_000000000046_0_5_3.py': Events._methods_ = [ COMMETHOD([dispid(202), 'propget'], HRESULT, 'ReferencesEvents', ( ['in'], POINTER(VBProject), 'VBProject' ), ( ['retval', 'out'], POINTER(POINTER(ReferencesEvents)), 'prceNew' )), COMMETHOD([dispid(205), 'propget'], HRESULT, 'CommandBarEvents', ( ['in'], POINTER(IDispatch), 'CommandBarControl' ), ( ['retval', 'out'], POINTER(POINTER(CommandBarEvents)), 'prceNew' )), ] So, it seems we need a POINTER(VBProject). VBProject is a CoClass, implementing the _VBProject COM interface. Further grepping shows the comtypes\gen\VBIDE.py is an alias module for comtypes\gen\_0002E157_0000_0000_C000_000000000046_0_5_3.py, so we use the former because it is much easier to type: >>> from comtypes.gen import VBIDE >>> from ctypes import POINTER >>> acc.VBE.Events.ReferencesEvents(POINTER(VBIDE._VBProject)()) <POINTER(_ReferencesEvents) ptr=0x274f2c at 11c7490> >>> (The idiom 'POINTER(VBIDE._VBProject)()' creates a NULL-pointer to _VBProject) And finally we try to find some events: >>> ShowEvents(acc.VBE.Events.ReferencesEvents(POINTER(VBIDE._VBProject)())) # event found: _dispReferencesEvents_ItemAdded # event found: _dispReferencesEvents_ItemRemoved <comtypes.client._events._AdviseConnection object at 0x011BACB0> >>> Now, I do not know what I have to do to trigger these events, but it looks like the code could possibly work. Thomas _______________________________________________ python-win32 mailing list python-win32@python.org http://mail.python.org/mailman/listinfo/python-win32