How to catch exception in report export (file already open)
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;
<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;