As with most (if not all) high-level languages, it can be all too easy
to churn out code that is unbearably slow, but that can be easily
fixed with a little thought.
With
WIN32OLE
, you need to be careful with unnecessary dynamic
lookups. Where possible, it is better to assign a
WIN32OLE
object
to a variable and then reference elements from it, rather than
creating a long chain of ``.'' expressions.
For example, instead of writing
workbook.Worksheets(1).Range("A1").value = 1
workbook.Worksheets(1).Range("A2").value = 2
workbook.Worksheets(1).Range("A3").value = 4
workbook.Worksheets(1).Range("A4").value = 8
|
we can eliminate the common subexpressions by
saving the first part of the expression to a temporary
variable and then make calls from that variable:
worksheet = workbook.Worksheets(1)
worksheet.Range("A1").value = 1
worksheet.Range("A2").value = 2
worksheet.Range("A3").value = 4
worksheet.Range("A4").value = 8
|