Recent

Author Topic: Opening file using Excel Worksbooks.Open gives runtime exception  (Read 1389 times)

fred

  • Full Member
  • ***
  • Posts: 208
When compiling an application made in Laz 2.0.8 32bit / fpc 3.0.4 / Win 7 opening an Excel file works.
In Laz 2.0.10 64 bit / fpc 3.2.0 / Win 7 WorkBooks.Open gives an error, see then next lines:
Quote
[Debugger Exception Notification]

Project ExcelTest raised exception class 'EOleError' with message:
Method 'Open' is not supported by automation object

 At address 1001988D6

[Ignore this exception type]

[Break] [Continue]

This is a part of the code I modified for testing:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.ExcelOpenen(fname: string);
  2. var
  3.   ExcelApp: OleVariant;
  4.   xlw: OleVariant;
  5.   xls_bestand: WideString;
  6. begin
  7.   fname := 'x:\Stationstabel - 141215.xlsx'; // just an Excel file
  8.   xls_bestand := WideString(fname);
  9.  
  10.   ExcelApp := CreateOleObject('Excel.Application');
  11.   try
  12.     try
  13.       // this lines raises the exception
  14.       xlw := ExcelApp.WorkBooks.Open(FileName:=xls_bestand, Notify:=False, ReadOnly:=True);
  15.     except
  16.       on E: Exception do begin
  17.         ShowMessage(E.Message + ',' + fname);
  18.       end;
  19.     end;
  20.   finally
  21.     ExcelApp.ActiveWorkbook.Saved := True;
  22.     ExcelApp.Quit;
  23.   end;
  24. end;
  25.  

I have tried the 64 bit and compile to 32 bit win32/i386.
Does anybody have an idea what the reason for this error is and how to fix it?


wp

  • Hero Member
  • *****
  • Posts: 13585
Re: Opening file using Excel Worksbooks.Open gives runtime exception
« Reply #1 on: July 30, 2020, 02:23:35 pm »
When compiling an application made in Laz 2.0.8 32bit / fpc 3.0.4 / Win 7 opening an Excel file works.
In Laz 2.0.10 64 bit / fpc 3.2.0 / Win 7 WorkBooks.Open gives an error, see then next lines:
Quote
Project ExcelTest raised exception class 'EOleError' with message:
Method 'Open' is not supported by automation object
[Ignore this exception type]

I tried your code in Laz trunk / FPC 3.0.4 / 32-bit, and it is working, and it tried it in Laz trunk / FPC 3.2.0 / 32-bit, and I get the same error as you. Looks like a bug in FPC 3.2.0...

Depending on what you want to do with the Excel file it may be easier to use fpspreadsheet to open and read the file; it works without Automation and is cross-platform,
See the documentation  or examples, or numerous posts in the fpspreadsheet sub-board of this forum

[P.S]
I can open the file with your code using FPC 3.2.0 as well when I remove the named arguments in the "Open" call and use only:
Code: Pascal  [Select][+][-]
  1. xlw := ExcelApp.WorkBooks.Open(xls_bestand);
« Last Edit: July 30, 2020, 02:29:20 pm by wp »

fred

  • Full Member
  • ***
  • Posts: 208
Re: Opening file using Excel Worksbooks.Open gives runtime exception
« Reply #2 on: July 30, 2020, 02:40:21 pm »
Thanks wp, I was already afraid of that...
Most reading and writing for Excel I change for fpspreadsheet when I have to change the program.
This is part of where an Excel file is used as an template, i'm not sure if the formatting/formula is preserved.
I will take a look at it and make a test later, if it doesn't works I can install Laz 2.0.8 again.

PascalDragon

  • Hero Member
  • *****
  • Posts: 6398
  • Compiler Developer
Re: Opening file using Excel Worksbooks.Open gives runtime exception
« Reply #3 on: July 30, 2020, 02:53:16 pm »
It's already reported here.

fred

  • Full Member
  • ***
  • Posts: 208
Re: Opening file using Excel Worksbooks.Open gives runtime exception
« Reply #4 on: July 30, 2020, 03:51:24 pm »
Thanks.
I changed it to:
Code: Pascal  [Select][+][-]
  1. Workbooks.Open(xls_bestand, False, True, , , , , , , , False);
and it works :)

 

TinyPortal © 2005-2018