EAccessViolation when changing FontColor for multiple cells

Questions and answers on how to use XLSReadWriteII 5.
Post Reply
d3nton
Posts: 133
Joined: Thu Oct 25, 2012 9:48 am

EAccessViolation when changing FontColor for multiple cells

Post by d3nton »

Hi!

I got an EAccessViolation in function TXc12XF.GetFont when changing the FontColor for multiple cells (XLSReadWriteII 5.10.02).
I am not sure but it seems that this error only occurs if I am using the old xls format.

Sample code:
var
ExcelFile5 : TXLSReadWriteII5;
lindex :Integer;
begin
ExcelFile5 := TXLSReadWriteII5.Create(nil);
ExcelFile5.Version := Xc12Utils5.xvExcel97; <---- using old xls format
ExcelFile5.Filename := 'D:\output.xls';
for lIndex := 0 to 1000 do begin
ExcelFile5[0].AsString[0,lIndex] := 'test';
ExcelFile5[0].Cell[0,lindex].FontColor := $5050FF; <------- this results in an EAccessViolation
end;

ExcelFile5.Write;
end;

Any help would be appreciated.
Thank you.
larsa
Site Admin
Posts: 926
Joined: Mon Jun 27, 2005 9:30 pm

Re: EAccessViolation when changing FontColor for multiple cells

Post by larsa »

Hello

Don't use the Cell object for formatting several cells. It's only usefull for just formatting a few cells, or read the format of cells.
Use CmdFormat instead. Example:

Code: Select all

  for i := 0 to 1000 do
    XLS[0].AsString[1,i] := 'Test';

  XLS.CmdFormat.BeginEdit(XLS[0]);
  XLS.CmdFormat.Font.Color.RGB := $5050FF;
  XLS.CmdFormat.Apply(1,0,1,1000);
Lars Arvidsson, Axolot Data
d3nton
Posts: 133
Joined: Thu Oct 25, 2012 9:48 am

Re: EAccessViolation when changing FontColor for multiple cells

Post by d3nton »

Hi!

Thank you! Works great.
Unfortunately I have a similar problem when trying to convert multiple cells into date format. I get the same EAccessViolation.
I know, I should not access the cells directly for formatting multiple cells but I don't find a function similar to the cmdformat which converts the cells into date format:

Sample
var
ExcelFile5 : TXLSReadWriteII5;
lindex :Integer;
begin
ExcelFile5 := TXLSReadWriteII5.Create(nil);
ExcelFile5.Version := Xc12Utils5.xvExcel97;
ExcelFile5.Filename := 'D:\output.xls';
for lIndex := 0 to 1000 do begin
ExcelFile5[0].AsDateTime[0,lindex] := date; <- EAccessViolation
end;

ExcelFile5.Write;
end.

Is there a function (similar to the cmdformat) which can be used for this?
Thank you.
larsa
Site Admin
Posts: 926
Joined: Mon Jun 27, 2005 9:30 pm

Re: EAccessViolation when changing FontColor for multiple cells

Post by larsa »

Hello

This is fixed in update 5.10.06
Lars Arvidsson, Axolot Data
d3nton
Posts: 133
Joined: Thu Oct 25, 2012 9:48 am

Re: EAccessViolation when changing FontColor for multiple cells

Post by d3nton »

Thank you.
The EAccessViolation issue seems to be fixed. But now I got a corrupted xls file with the code above.
Opening the created xls file in Excel results in the following message: Protected view - 'Office has detected a problem with the file. Editing it may harm your computer. Click more for details'.
larsa
Site Admin
Posts: 926
Joined: Mon Jun 27, 2005 9:30 pm

Re: EAccessViolation when changing FontColor for multiple cells

Post by larsa »

Hello

FIxed in update 5.10.07
Lars Arvidsson, Axolot Data
Post Reply