Home General

Print to file with file open crashes app

edited October 2010 in General
When I choose print to file and the output file is open in another
application ReportBuilder crashes my app. For example if the user selects
export to XLS data file and the file is open already in excel then my app
will crash. I'm wondering where is a good place to put a try..except block
so I can catch this error and display a dialog instead of crashing the app?

Thanks in advance,
Rodger Van Kirk

Comments

  • edited October 2010
    I forgot to mention I'm using RB 12.02 and D2007

    Rodger Van Kirk

  • edited October 2010
    Hi Rodger,

    I'm a bit unclear about what you mean by "crashes my app". Does your
    application stop working and quit or are you getting an EFCreateError or
    EPrintError exception. Getting these exceptions is the default behavior
    while debugging in the Delphi IDE. When running your application as a stand
    alone exe or dll, you can suppress these exceptions by placing a
    try...except around the Report.Print command and check for this type of
    exception being thrown.

    --
    Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com

    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • edited October 2010
    I guess I should have been a little more clear about it. I am using
    MadException and an EPrintError exception is being fired off. I will try a
    try..except around the .Print call and see if it takes care of it.

    Thanks,
    Rodger Van Kirk

  • edited October 2010
    The exception happens when running the application not in the debugger. I
    also tried the try..except arount the .Print and it still happened.

    Regards,
    Rodger Van Kirk

  • edited October 2010
    Hi Rodger,

    What exactly would you like to happen in this case? Would you like your own
    personal message to display or would you like the user to simply see no
    error and move on? I assume you have something like the following...

    try
    Report.Print;
    except
    on E: EPrintError do
    ShowMessage('File Already Open');
    end;

    --
    Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com

    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • edited October 2010
    Very close. The only difference I did was:

    ShowMessage(E.Message);

    I would like to display some kind of message to the user so they can fix the
    issue and try again.

    Thanks in advance,
    Rodger Van Kirk


  • edited October 2010
    Hi Rodger,

    In your application with the try...except around the Print command, are you
    then receiving two messages when you run from a stand-alone exe? In my
    testing, I am just getting a single message (the ShowMessage that I
    created).

    --
    Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com

    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • edited November 2010
    I keep trying to determine why my try..except is not catching the exception.
    Could it be because I have set my modalPreview to false? I see the code has
    pasted the try..except well before I ever even try to print to file.

    Thanks in advance,
    Rodger Van Kirk



  • edited November 2010
    Hi Rodger,

    Yes, printing from the preview will prevent the exception from being
    intercepted.

    Another option would be to use a report event such as the
    Report.OnPrintDialogClose to first check if the file is open, then show a
    message and cancel if so. Something like the following...

    Note: The IsFileInUse is a routine you write to check if the file is open or
    not.

    procedure TForm1.ppReport1PrintDialogClose(Sender: TObject);
    begin

    if IsFileInUse(ppReport1.PrintDialog.TextFileName) then
    begin
    ShowMessage('File In Use');
    ppReport1.PrintDialog.ModalResult := mrCancel;
    end;

    end;



    Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
    "Rodger Van Kirk" wrote in message news:4ccf2fee$1@mail....

    I keep trying to determine why my try..except is not catching the exception.
    Could it be because I have set my modalPreview to false? I see the code has
    pasted the try..except well before I ever even try to print to file.

    Thanks in advance,
    Rodger Van Kirk



    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
This discussion has been closed.