[Q] win32com/Excel

Robin Becker robin at jessikat.fsnet.co.uk
Tue Sep 3 18:17:43 EDT 2002


In article <mailman.1031083266.9747.python-list at python.org>, Mike
Brenner <mikeb at mitre.org> writes
>Hi Robin, 
>
>That was great, but most importantly, it was FAST!
>
>Would it be possible to tell how you did that so quickly? Is there a reference 
>that would tell me that x1LastCell is in constants? or that you put the 
>activeWindow in front of ActiveCell?

OK let me tell you how I did this. I'm getting old so my memory is
fairly poor these days so I rely on the source of the interface file
generated by win32com\client\makepy.py. I scrolled down and built the
interface for my excel which appeared in the list as 'Microsoft Excel
9.0 Object Library (1.3)'.

The I edited the generated file in win32com\gen_py
it was called 00020813-0000-0000-C000-000000000046x0x1x3.py derived from
the GUID I guess.

Scanning I see the xlLastCell constant in a class called constants. I
don't normally use these constants so I checked what happens to
constants; right at the bottom I see

win32com.client.constants.__dicts__.append(constants.__dict__)

ie the constants are added into the win32com.client.constants thing's
dict so should appear as attributes of win32com.client.constants.

Then I looked for something to hang this constant on, seems like a range
has this method. The vb seemed to like using ActiveCell as the start
point and scanning for that method reveals a method  in a class called
Window.

As for the book, sheet and window things I seem to remember them from
some previous era when I did a lot of excel (horror show).


As for the nitty gritty details of how to turn off various bits of
interactive code I can only recommend detailed reading of any available
documentation.

My boss is Andy Robinson co-author with Marc Hammond of the win32 python
book so from the python side I know that's a very good start. On the
other side I recommend asking an expert or consulting the appropriate
newsgroup. Writing an application in excel obviously requires answers to
your questions. A quick check reveals that there is a way to stop
checking for viruses in macros (at least in my excel) that might be the
way to go.

Googling with 'microsoft project option' reveals a lot of hits so
perhaps that might be a good source.

>
>Do you know of any references as to how to do it in Microsoft Project whose 
>object model seems much less documented than Excel's is?
>
>I know how to use Project Explorer in the Visual Basic Editor inside the 
>Microsoft OLE applications like Microsoft Project, and I know how to get the 
>Python list of objects using makepy. However, that does not seem to be enough to 
>figure out many questions in Microsoft Project (and even some questions in 
>Microsoft Excel).
>
>What would be really nice is a document that explains what each object in the 
>list is used for. For example, when opening a Microsoft Project file, it asks a 
>bunch of questions like: are macros okay? should I open related files? should I 
>save the changes I think you made to the previous file? 
>
>I would like to learn how to cancel out those messages when I open a Microsoft 
>Project file from Python. What book or application do you recommend I purchase? 
>Or what file can I download to learn how to do these things?
>
>Thanks,
>Mike
>
>
>
>>I would like to emulate this Visual Basic statement from Python, using
>>Com:
>>
>>        Range(Range("A1"), ActiveCell.SpecialCells(xlLastCell)).Select
>>
>>this statement selects the active worksheet (which is, incidentally,
>>not similar to UsedRange).  
>>
>>any clues?  
>>
>>thank you,
>from win32com.client import Dispatch
>from win32com.client import constants
>xl = Dispatch("Excel.Application")
>AB = xl.ActiveWorkbook
>AS = AB.ActiveSheet
>AW = xl.ActiveWindow
>R = AS.Range('A1',AW.ActiveCell.SpecialCells(constants.xlLastCell))
>print AB.Name, AS.Name, R

-- 
Robin Becker



More information about the Python-list mailing list