Need Help with COM

Don Dwiggins dwig at advancedmp.net
Wed Aug 15 12:53:23 EDT 2001


> From: "Michael A. Howard" <mhoward at mahoward.com>

> I'm coming to Python from VB and trying to understand how the COM stuff
> works.

> I have a COM server (not written by me) that supports in VB the call
> oAppPC.AppItem.Properties.Item(1).Caption
> Which returns the caption of this item as I can see it from the object
> browser in VB

> The same call in python returns:
> Traceback (most recent call last):
>   File "<interactive input>", line 1, in ?
> AttributeError: 'tuple' object has no attribute 'Caption'

In cases like this, I find it useful to use the interactive window of
Pythonwin.  Do something like the following (top of the head; YMMV):

>>> from win32com.client import *
>>> oAppPC = Dispatch('{whatever}.AppPC')
>>> ai = oAppPC.AppItem
>>> ai

>>> props = ai.Properties
>>> props

>>> i1 = props.Item(1)
>>> i1
  {At this point, you'll probably see a tuple.  The first element is what
   you get using VB; following are the value(s) of any in/out argument(s)}
>>> c = i1[0].Caption
>>> c
  {this should be the caption you want -- if it's a string, it'll be in
    Unicode, indicated by the "u" at the beginning.}

At each stage, you display the variable you've just set, to see that it
looks reasonable.

It's worth noting that COM strings are always in Unicode; VB hides this from
you, but Python doesn't (although it will let you pass in a non-Unicode
string as an argument and it'll do the right thing).

Good luck,

-- 
Don Dwiggins                    "Solvitur Ambulando"
Advanced MP Technology
dwig at advancedmp.net





More information about the Python-list mailing list