How to catch exception in report export (file already open)

hsmhsm
edited 9:23AM in FastReport 4.0
In Delphi (2009) I'm using these lines to export a report using code, where I have frxReport1 and frxXMLExport1 components placed on the form.


<other code to set up queries etc...>
frxReport1.PrepareReport;
frxReport1.Export(frxXMLExport1);

This works OK but obviously raises an exception if the excel filename used for export is already open in Excel.

To get round this I tried using a try-except block like below but frxReport1.Export seems to cause a system wide exception first, before Delphi can catch it and so kills my program.
ie I get an exception saying 'file is open in another process' and the program halts so my own exception code never gets executed.

How can I generate a more user friendly message if the export causes an exception and allow the user to try a different filename?

This is the code I used to try to handle the exception

try

frxReport1.PrepareReport;
frxReport1.Export(frxXMLExport1); // if there is an exception in here, the 'on e.exception' code never runs
filesaved := true;

except

on e: exception do
begin
ShowMessage('Failed to save ' + frxXMLExport1.Filename +slinebreak + slinebreak +
'Are you sure you don''t already have this file open in Excel?');
filesaved := false;
end;

end;

Leave a Comment

Rich Text Editor. To edit a paragraph's style, hit tab to get to the paragraph menu. From there you will be able to pick one style. Nothing defaults to paragraph. An inline formatting menu will show up when you select text. Hit tab to get into that menu. Some elements, such as rich link embeds, images, loading indicators, and error messages may get inserted into the editor. You may navigate to these using the arrow keys inside of the editor and delete them with the delete or backspace key.