Excel Column Autofit from Delphi XE2

I am moving old code from Office 2000 to Office 2010 and have encountered the problem of getting Excel in Autofit columns

The code that worked is as follows:

for x := 1 to LV.Columns.Count do XLApp.Columns[x].EntireColumn.AutoFit; 

Where XLApp is a component of TExcelApplication

When I look at VBA, the equivalent code should be

  Columns("A:A").EntireColumn.AutoFit 

Itโ€™s easy for me to change the code so that the range matches VBA, but this does not seem to be a problem. The error returned by the compiler is -

 Class does not have a default property 

And the highlight [x] highlighted. Code completion offers no way to select a single column, either an integer or a range.

Any suggestions?

+4
source share
1 answer

The code you have will work for late bound COM. But you are claiming to use early related COM.

For early related COM, you need to use something like this:

 ExcelApp.Range['A1','A1'].EntireColumn.AutoFit; 

I canโ€™t explain how the code worked in the past. Perhaps the import block created from the Excel type library was slightly different.

+6
source

All Articles