2
votes

I just installed Delphi XE3. The previous version was XE. My program generates Excel files (I am using Office 2013). I imported Excel Type Library. When I using Delphi XE then this code is working. When I using Delphi XE3 then I got an error message, something like this: 'HorizontalAlignment property can not be set' What is changed in XE3?

Here is the code:

  VAR Myworkbook, range, excelapp : olevariant;
  Begin
   ExcelApp:=CreateOleObject('Excel.Application');
   ExcelApp.Visible:=true;
   MyWorkbook:=ExcelApp.Workbooks.Add;
   MyWorkbook.Activate;
   MyWorkbook.Activesheet.PageSetup.Orientation := xlPortrait;
   MyWorkbook.Activesheet.PageSetup.PaperSize := xlPaperA4;
   MyWorkbook.Activesheet.PageSetup.LeftMargin := CMtoPT(1);
   MyWorkbook.Activesheet.PageSetup.RightMargin := CMtoPT(1);
   MyWorkbook.Activesheet.PageSetup.TopMargin := cmtopt(1.5);
   MyWorkbook.Activesheet.PageSetup.BottomMargin := cmtopt(1.5);


  Range:=ExcelApp.Range['A1','A1'];

   Range.HorizontalAlignment := xlLeft;
   Range.VerticalAlignment := xlCenter;
 END
2
I am using EXCEL_TLB. And it is working in XE. With Excel 2007-2013 as well. But, what would you suggest to use anyway?Gábor Pető
Seems odd. What is xlLeft? What is the exact error?David Heffernan
It is hard to translate: alcaportal.hu/error.jpgGábor Pető
xlLeft arrange the text to left. If I right click on it and find declaration it goes to EXCEL_TLB. But I think delphi has a problem with HorizontalAlignment .Gábor Pető
@GáborPető you should post your answer as an answer to your own question so other people might find it useful, and people like me won't try to answer your question after it has been answered!Toby Allen

2 Answers

1
votes

I have the solution:

Range.VerticalAlignment := xlCenter 

should be:

Range.VerticalAlignment := integer(xlCenter)

I have to convert the constant explicitly to integer. Here is where I found it: http://forums.embarcadero.com/thread.jspa?threadID=106493

0
votes

I think because you are using late binding eg Declaring ExcelApp as an olevariant

 VAR Myworkbook, range, excelapp : olevariant;
 Begin
 ExcelApp:=CreateOleObject('Excel.Application');

Then anything you call on ExcelApp that is incorrect is handled by Excel rather than Delphi. This means that either HorizontalAlignment is a not settable property, or xlLeft is in some way incorrect.

It is unlikely this is anything to do with your version of Delphi.