Dan Kelly
Dan Kelly

Reputation: 2702

Excel Column Autofit from Delphi XE2

I'm moving some older code from Office 2000 to Office 2010 and have ran into an issue with getting Excel to Autofit Columns

The code that was working is as follows:

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

Where XLApp is a TExcelApplication component

When I look at VBA the equivalent code should be

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

It's easy for me to change my code so that the range matches the VBA, but that doesn't seem to be the problem. The error returned by the compiler is

Class does not have a default property

And the [x] is highlighted. Code completion does not offer any way that I can see of selecting an individual column, either as an integer or a Range.

Any suggestions?

Upvotes: 4

Views: 6908

Answers (1)

David Heffernan
David Heffernan

Reputation: 612954

The code that you have will work for late bound COM. But you state that you are using early bound COM.

For early bound COM you need to use something like this:

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

I cannot explain how the code worked in the past. Perhaps the import unit created from the Excel type library differed in some subtle way.

Upvotes: 7

Related Questions