Lazarus and Excel formulas via OLE

Hi there,

I am trying to export data to Excel from a Lazarus application using OLE.
When I try to add a value in a cell it works, but as soon as I try to add a formula in a cell I receive an error message.

procedure TForm1.Button1Click(Sender: TObject);
var XLApp: OLEVariant;
    x, y: Byte;
    tmpValue: Variant;
begin
  // bron: http://wiki.lazarus.freepascal.org/Office_Automation
  XLApp := CreateOleObject('Excel.Application');
  try
    XLApp.Visible := true;
    XLApp.DisplayAlerts := False;
    XLApp.Workbooks.Add; // alternatief: XLApp.Workbooks.Open(filename);

    // Column headers...
    For x := 0 to Query1.FieldCount-1 do begin
      tmpValue := Query1.FieldDefs.Items[x].Name;
      XLApp.Cells[1, x+1] := tmpValue;
    end;

    tmpValue := '% actual/max';
    XLApp.Cells[1, x+2].Value := tmpValue;

    // Data...
    y := 2;
    while not Query1.Eof do begin
      for x := 0 to Query1.FieldCount-1 do begin
        tmpValue := Query1.Fields[x].Value;
        XLApp.Cells.Item[y, x+1].Value := tmpValue;
      end;

      tmpValue := 'IF(I2<>0;(100*J2)/I2;0)';
      XLApp.Cells[y, x+3].Value := tmpValue;

      inc(y);
      Query1.Next;
    end;

    // Formatting...
    XLApp.Range['A1:XFD1'].Font.Bold := true;
    XLApp.Range['A1:XFD1'].EntireColumn.AutoFit;

  finally
    XLAPP := Unassigned;
  end;
end;   

Open in new window

The problem code is at line 30 and 31.

What is going wrong here? I'm working in a Windows environment.

Thank you for your help!

Kind Regards,
Stefan
Stefan van RoosmalenfounderAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rgonzo1971Commented:
Hi,

maybe try

      tmpValue := '=IF(I2<>0,(100*J2)/I2,0)';
      XLApp.Cells[y, x+3].Formula:= tmpValue;

Open in new window


Regards
Stefan van RoosmalenfounderAuthor Commented:
This does not work unfortunately. Same error message.
Rgonzo1971Commented:
Text of the error?
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Stefan van RoosmalenfounderAuthor Commented:
Error message:
"Project ExcelApp raised exception class 'EOleException' with message: ###(gdb unparsed remainder:s 0x0 out of bounds>)###"

A funny detail: when I change line 30 from
tmpValue := '=IF(I2<>0;(100*J2)/I2;0)';

Open in new window

to
tmpValue := 'IF(I2<>0;(100*J2)/I2;0)';

Open in new window

then I do not have an error but then I do not have calculations. The formula in that case is displayed as text.
Rgonzo1971Commented:
pls change the semicolons in the formula to commas

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Stefan van RoosmalenfounderAuthor Commented:
Hey! That seems to be the trick! Wow...
Thank you very much you are making my day!
Rgonzo1971Commented:
Not knowing Lazarus maybe if you use FormulaLocal you could use semicolons
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.