Excel COM automation
vze4rx4y at verizon.net
Fri Mar 14 06:46:21 CET 2003
It should work, but if you're going to have multiple tasks
access Excel, it is like any other resource and needs to be
handled in a threadsafe way.
> We have a trivially simple Python app in production that loads Excel
> through COM, opens an Excel file, and saves it as tab-delimited. Today
> we had a silent failure, and tracked it down to two instances of our app
> trying to use the Excel object at the same time. Is this supposed to
> work (and if so, what steps are we missing)? And if it's not supposed to
> work, is there a way to determine that the COM object is "busy"?
Excel should respond to multiple COM requests; however,
you should do it in a threadsafe manner.
Many types of access change Excel's state. For instance,
it is common to set an activesheet and then write to the
sheet, but another task may select another sheet leaving
the first task writing on the wrong tab.
One solution is to rewrite the lines that alter state.
ActiveSheet.Range('a1:a2') = data
Sheet('input').Range('a1:a2') = data
This solution can involve a lot of changes to an already
Another solution is to implement some sort of resource
locking. COM probably provides for this but I don't
know the details. You can roll your own by agreeing
on a common sheet or cell and setting an in/use flag.
Be sure you free the flag when you're done or else
deadlock will occur. Instead of a flag, you can just
check to see if Excel is already open (does it give a
non-null value for ActiveSheet).
More information about the Python-list