Radu Adrian Ciora schrieb: > Hi, > I was trying to generate MSWord's commandbar's interface with the following > code: > > def generate_COM_interface(self, fname): > global number > def test(self): > try: > comtypes.typeinfo.LoadTypeLibEx(fname) > except WindowsError: > return comtypes.client.GetModule(fname) > > > and then call it like this: > l_i_face = self.generate_COM_interface("C:"+os.sep+"Program > Files"+os.sep+"Common Files"+os.sep+"Microsoft > Shared"+os.sep+"OFFICE11"+os.sep+"MSO.dll") > > but this returns None! > > Can anyone tell me where I'm wrong?
It returns None because the except clause does not trigger (LoadTypeLibEx succeeds), and there is no explicit 'return' statement executed. BTW: instead of adding all the strings with os.sep between them you should better write generate_COM_interface(r"c:\Program Files\Common Files\Microsoft Shared\OFFICE11\MSO.dll") or, even more portable, using the 'CommonProgramFiles' environment variable: import os generate_COM_interface(os.path.join(os.environ["CommonProgramFiles"], r"Microsoft Shared\OFFICE11\MSO.dll")) But what do you want to achieve? comtypes.typeinfo.LoadTypeLibEx(fname) returns an ITypeLib pointer which you probably do not need. comtypes.client.GetModule(fname) is what you need - it creates the typelib wrapper which contains the interface definitions. The GetModule() call returns the Python module, but it is probably better to import it with its short name that GetModule() also creates and prints! For me, the name seems to be comtypes.gen.Office. So I would write: # Create the typelib wrapper (if it does not yet exist): GetModule(os.path.join(os.environ["CommonProgramFiles"], r"Microsoft Shared\OFFICE11\MSO.dll")) # Import the typelib wrapper from comtypes.gen import Office # Use it print Office.CommandBars, Office.CommandBarControl You might have to adjust the above code a bit, I don't have OFFICE11 on my machine, only OFFICE10. 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