Recent

Author Topic: [SOLVED] problem with compiler : Fatal: Syntax error, "identifier" expec  (Read 24125 times)

nicola69

  • New Member
  • *
  • Posts: 15
Hi
I need read and write excel 2010 files from my lcl application.
I try to use OLE.

I write this simple application

procedure TForm1.Button1Click(Sender: TObject);
Var   XLApp,XLSHEET: OLEVariant;
      lastrow : integer;

begin
  XLApp := CreateOleObject('Excel.Application'); // requires comobj in uses
 try
   XLApp.Visible := False;         // Hide Excel
   XLApp.DisplayAlerts := False;
   if not OpenDialog1.Execute then exit;

   XLApp.Workbooks.Open(OpenDialog1.FileName);
   XLSHEET :=  XLApp.Workbooks.Sheets(0).Select;
   lastrow:= XLSHEET.Range('A65536').End(xlUp).Row;
  finally
 end;                           


fpc don't generate exe because i receive an error in lastrow:= XLSHEET.Range('A65536').End(xlUp).Row;
My error is unit1.pas(48,38) Fatal: Syntax error, "identifier" expected but "END" found

how can I resolve this error ?

« Last Edit: January 10, 2013, 11:03:58 am by nicola69 »

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
You have to add one "end;" at the end.
The valid construction is:
Code: [Select]
  try
  ...
  finally
  ...
  end;
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12797
  • FPC developer.
   XLSHEET :=  XLApp.Workbooks.Sheets(0).Select;
   lastrow:= XLSHEET.Range('A65536').End(xlUp).Row;

Shouldn't that all be square brackets  [] instead of round () ones?

nicola69

  • New Member
  • *
  • Posts: 15
You have to add one "end;" at the end.
The valid construction is:
Code: [Select]
  try
  ...
  finally
  ...
  end;

Yes i have add it but i have the same error.

nicola69

  • New Member
  • *
  • Posts: 15
   XLSHEET :=  XLApp.Workbooks.Sheets(0).Select;
   lastrow:= XLSHEET.Range('A65536').End(xlUp).Row;

Shouldn't that all be square brackets  [] instead of round () ones?

I have change round brackets with square but i have same problem.

I have attached a screenshot with this error.


BlueIcaro

  • Hero Member
  • *****
  • Posts: 834
    • Blog personal
Hi, add a
Quote
end;
after the finally,  see the vertical lines, in the code window. You can see that the
Quote
end;
after the finally belongs to the
Quote
begin
of the procedure.
So, as said Blaazen, the correct syntax is Try... finally ... end;
So your procedure should be like this:
Code: Pascal  [Select][+][-]
  1. procedure MiProcedure();
  2. Var
  3.  //put your variable here
  4. Begin
  5.   Try
  6.     //Put code here;
  7.   Finally
  8.    //Even you don't put any code here, you must put the end
  9.   end; //Always you must put the end;
  10. end; //This end; belongs to the "begin"
  11.  

In other way, if you don't put any code after the finally, why do you use it?

/BlueIcaro

Edit:  I can't write you code in my lazarus,
Quote
XLApp := CreateOleObject('Excel.Application');
   
    is a IDispatch, and It doesn't have the property workbooks. So I think it will be better if you attach you code at the post.
« Last Edit: January 09, 2013, 02:27:31 pm by BlueIcaro »

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
In other way, if you don't put any code after the finally, why do you use it?
Umm.. "try..end" structure doesn't exist as far as i know. "try..except..end" or "try..finally..end" are only ones i've heard of, and considered it ok to leave final section empty.

BlueIcaro

  • Hero Member
  • *****
  • Posts: 834
    • Blog personal
In other way, if you don't put any code after the finally, why do you use it?
Umm.. "try..end" structure doesn't exist as far as i know. "try..except..end" or "try..finally..end" are only ones i've heard of, and considered it ok to leave final section empty.
It's true, I wantted to say, that if don't use any code after finally, why use Try..Finally struct.  :-[

/blueIcaro

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Quote
It's true, I wantted to say, that if don't use any code after finally, why use Try..Finally struct.
Maybe his code is under development, he can add something later.

Anyway, I cannot test the code because I am on Linux (and LazActiveX package needs unit Windows to compile).

Now it seems to me that compiler gives the same message ("Fatal: Syntax error, "identifier" expected but "END" found") for both
1) missing end in try..finally construction
2) this code:    lastrow:= XLSHEET.Range('A65536').End(xlUp).Row;

Definitely, there should be square braces for Range (See here: http://www.borlandtalk.com/excel-range-sort-question-vt57774.html)
but I don't know how to solve the end problem.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Quote
but I don't know how to solve the end problem.

FPC allows redefinition of Pascal reserved words by prepending an & to the word.
So it may be that substituting &end would work?
It's worth a try.

nicola69

  • New Member
  • *
  • Posts: 15
Re: problem with compiler : unit1.pas) Fatal: Syntax error, "identifier" expec
« Reply #10 on: January 10, 2013, 11:03:12 am »
Quote
but I don't know how to solve the end problem.

FPC allows redefinition of Pascal reserved words by prepending an & to the word.
So it may be that substituting &end would work?
It's worth a try.

Eureka

I have replaced end with &end and i have replaced xlup with value -4162.

Now, fpc compile my program without error.

Thank howardpc





HJE

  • Newbie
  • Posts: 2
Re: [SOLVED] problem with compiler : Fatal: Syntax error, "identifier" expec
« Reply #11 on: January 04, 2023, 04:32:30 am »
I have just joined this forum to say thanks to howardpc.
Brilliant idea to put a & in front of the End!  :-)

In my case the problem was to define a range on the first 3 rows of a Table in a Word document, like this >>
Code: Pascal  [Select][+][-]
  1. sR := MyDoc.Range(MyDoc.Tables.Item(1).Rows.Item(1).Range.Start, MyDoc.Tables.Item(1).Rows.Item(3).Range.End);

That works in Delphi but not in Lazarus.
Again you need to replace: … .Range.End  with: ... .Range.&End.

Bonus information:
I needed this range to go like this >>
Code: Pascal  [Select][+][-]
  1. sR.Rows.HeadingFormat := True;
What that does is (for a large table that goes across several pages) it repeats the (3-row) header of your table on each page. Very useful!

« Last Edit: January 09, 2023, 03:45:01 am by HJE »

 

TinyPortal © 2005-2018