Thread: Com

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    56
    Rep Power
    12

    Com


    I'm trying to access some COM objects with Python. I read some sample code with the following line:

    o = win32com.client.Dispatch("Object.Name")

    eg.
    o = win32com.client.Dispatch("Excel.Application")

    How do I know what I should put as "Object.Name"? I couldn't find anything from the doc of my COM objects.
  2. #2
  3. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407
    you mean..
    Code:
    import win32com
    print dir( win32com )
    there you have mostly everything, run the code
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    Originally Posted by oli
    How do I know what I should put as "Object.Name"? I couldn't find anything from the doc of my COM objects.
    It should be stated in the docs for the COM object, but it often isn't (M$ docs are particularly bad for this).

    There is one way I know to find out, but it is a bit long-winded and there may well be easier ways.

    1) open the PythonWin IDE and select the "tools/COM makepy utility" menu entry.

    2) select the type library that you want in the listbox and click OK. This will generate the stub file for that type library.

    3) navigate to Python\Lib\site-packages\win32com\gen_py and open the .py file you have just generated. The files are named after the library GUID, so it is easiest to use the timestamp to find the most recent file.

    4) The stub file will have a class derived from 'CoClassBaseClass' for each class in the type library. The comment just before the class definition gives the name that you want. e.g.

    Code:
    from win32com.client import CoClassBaseClass
    # This CoClass is known by the name 'Excel.Application.8'
    class Application(CoClassBaseClass): # A CoClass
    	CLSID = IID('{00024500-0000-0000-C000-000000000046}')
    	coclass_sources = [
    		AppEvents,
    	]
    	default_source = AppEvents
    	coclass_interfaces = [
    		_Application,
    	]
    	default_interface = _Application
    Generating the stubs has other advantages too. It not only speeds up accessing the COM objects, but it also enables you to query the interfaces in interactive mode. e.g.

    Code:
    >>> app = win32com.client.Dispatch("Excel.Application")
    >>> dir(app)
    ['ActivateMicrosoftApp', 'AddChartAutoFormat', 'AddCustomList', 'CLSID', 'Calculate', 'CentimetersToPoints',...]
    >>>
    Dave - The Developers' Coach
    Last edited by DevCoach; June 4th, 2004 at 03:46 PM.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    I should add that not every class will have the comment at the start - I think it is only those that are intended to be creatable directly by calls to win32com.client.Dispatch(). Other classes will be generated only by method calls on existing objects.

    Note that for Excel at least, it is not always necessary to start by creating the Application object. If you create a Sheet object then the Application object will be automatically created along with a single Sheet, and can be accessed by the Sheet.Application property.

    Dave - The Developers' Coach
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    56
    Rep Power
    12
    There's a line like this in the .py file that the makepy utility generated.

    # This CoClass is known by the name 'SMIEngine.SMIHost.1'

    I tried
    w=win32com.client.Dispatch("SMIEngine.SMIHost")
    but I got the following error message

    Traceback (most recent call last):
    File "<interactive input>", line 1, in ?
    File "C:\Python23\Lib\site-packages\win32com\client\__init__.py", line 95, in Dispatch
    dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch,userName,clsctx)
    File "C:\Python23\Lib\site-packages\win32com\client\dynamic.py", line 91, in _GetGoodDispatchAndUserName
    return (_GetGoodDispatch(IDispatch, clsctx), userName)
    File "C:\Python23\Lib\site-packages\win32com\client\dynamic.py", line 79, in _GetGoodDispatch
    IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch)
    com_error: (-2147467262, 'No such interface supported', None, None)

    Did I do anything wrong?

    Thank you.

IMN logo majestic logo threadwatch logo seochat tools logo