COM and win32 - casting com to string

Tim Golden tim.golden at iname.com
Wed Oct 3 16:32:36 CEST 2001


"Paul Brian" <pbrian at demon.net> wrote in message news:<1002093124.20881.0.nnrp-14.c2de2503 at news.demon.co.uk>...
> the code:
>     x = excelAppl.ActiveWorkBook.Names
>     for namerange in x:
>         print namerange
> gives me my two ranges back:
> =Sheet1!$D$3:$G$3
> =Sheet1!$D$4:$D$7
> 
> all I want to do know is split those into the sheet1 and the $D$3....
> 
> however the two results are not python instances but
> <COMObject <unknown>>
> 
> I cannot perform string operations, repr does not help and type() just tells
> me they are "instances" - can anyone tell me what I am doing wrong.

I don't claim to be an expert here, but the following two things might
help you out. You probably need both, tho' I don't have time to check
at the moment.

1) Run gen_py against the Excel application library (from Tools > COM
Makepy if you're running ActivePython, directly otherwise). This will
generate a .py file containing the COM interface to Excel which will
let Python give slightly more useful information such as the name of
the class whose instance you are listing.

2) Use the RefersTo property of the items in the namerange:

>>> x = excelAppl.ActiveWorkbook.Names
>>> 
>>> true_range = x[0].RefersTo
>>> print true_range
=Sheet1!$A$1:$A$3

I'm using Excel 2000 which may make some difference to names
(specifically, I had to use "ActiveWorkbook" (lowercase b) rather than
"ActiveWorkBook" as in your example. I hope, tho', that the technique
still applies.

Tim.



More information about the Python-list mailing list