How to set a default filename to export a report to any format?

edited February 2020 in FastReport VCL 5

I have to set a default filename in script to export a report to any available format, using a dataset field value, like:

FileName := 'MyReport_' + DataSet.FieldByName('number').AsString

So, the filename would be MyReport_123.pdf, MyReport_123.jpg or MyReport_123.doc.

Comments

  • Not sure I understand the question.

    Where exactly have you set the "FileName"? Is it a local variable?


    Assuming it is local, to export in PDF, DOCx, RTF etc. you need to have instantiated the Export Components beforehand, and then you would need something like:


    frxPDFexport1.FileName := locFileName + '.pdf';

    frxReport1.Export (frxPDFExport1);


    frxRTFexport1.FileName := locFileName + '.rtf';

    frxReport1.Export (frxRTFexport1);


    frxDOCXexport1.FileName := locFileName + '.docx';

    frxReport1.Export (frxDOCXexport1);


    In our projects we have a single Unit which encapsulates this, so the code is common and just requires one property to be set and one method to be called...


    Cheers, Paul

  • edited February 2020

    Hi Paul,

    My sample code was just as I was thinking about how it could work, with "FileName" as a property of some object, like TfrxReport or other.

    I know about the FileName property of each export component (PDF, DOCx, etc) and I'm using it in some cases. I liked your solution.

    But, the user can preview two or more reports at same time, so I have to set the FileName in exports components for each report. The user will export the report at preview window.

    But, I think the exports components are global, like singletons. Or I'm wrong?

  • [quote]But, I think the exports components are global, like singletons. Or I'm wrong?[/quote]


    AFAIK you are right. Assuming you just drop the Export Components on your "main" (or Global Functions) Form.


    I haven't tried it, but wonder if you could instantiate multiple Export Components in your code.


    You'd need to declare them "locally" and then destroy / free them at an appropriate point too.


    frxPDFexport1 : TfrxPDFexport;

    frxPDFexport2 : TfrxPDFexport;

    : :

    frxPDFexport1 := TfrxPDFexport.Create (nil);

    frxPDFexport2 := TfrxPDFexport.Create (nil);

    : :

    frxPDFexport1.Free;

    frxPDFexport2.Free;


    Each Instance could then have its appropriate FileName...


    Good luck!


    Cheers, Paul

  • PolomintPolomint Australia
    edited February 2020

    D'uh!


    Actually I don't think there is anything stopping you dropping multiple instances of the "same" Export Component on your "main" Form - so no need to do it in code.


    But I've realised that doing this might "upset" Preview, and if it doesn't break anything, I don't know what happens if the User chooses to Export from within Preview.


    Could be worth building yourself a "testbed" to play with this a bit...


    Cheers, Paul

Leave a Comment