Skip to main content

Notifications

Microsoft Dynamics NAV (Archived)

Save .txt File

Posted on by 2,558

Hi All,

How can I save the text file instead of prefix the file name (refer to picture below) in report? Such as let user to save their own .txt file in the particular folder location or prompt a window to request for "Open" , "Save" or "Cancel"?

Best Regards,

Vanessa

*This post is locked for comments

  • ATNA Profile Picture
    ATNA 5 on at
    RE: Save .txt File

    hi,

    how to solve this error ?

  • Mitr Profile Picture
    Mitr 182 on at
    RE: Save .txt File

    Above video really helped me.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Save .txt File

    Hi Vanessa,

    how you solved this issue ?

    Thank you

    Gero

  • Suggested answer
    4BzSoftware Profile Picture
    4BzSoftware 6,071 on at
    RE: Save .txt File

    Hi Vanessa,

    1. To export the details with separator, you can use FORMAT function to convert decimal to text before writing to file, for example, gFil_File.WRITE(FORMAT(Amount)); There are parameters to help you get desired format msdn.microsoft.com/.../dd301367(v=nav.70).aspx

    2. To export all details of TotalAmount from VAT Statement:

    2.1. Prepare server file OnPreReport instead of OnPostReport, move below code to the end of OnPreReport:

    //Server side: process data with temporary file

    gTxt_ServerFileName := gCoU_FileMgmt.ServerTempFileName('txt');

    gFil_File.CREATE(gTxt_ServerFileName);

    gFil_File.TEXTMODE(TRUE);

    2.2. Add VAT Entry data item and handle the OnAfterGetRecord trigger.

  • Vanessa Shin Profile Picture
    Vanessa Shin 2,558 on at
    RE: Save .txt File

    Hi,
    Thousand thanks for your help.
    BTW, may I know how to export all details of TotalAmount from VAT Statement and export the details with separator? I have no idea on this...

    E.g.:

  • Verified answer
    4BzSoftware Profile Picture
    4BzSoftware 6,071 on at
    RE: Save .txt File

    Hi Vanessa,

    I send you sample report object in text format:

    - Report Request Page with Client File Name and Open After Download option.

    - OnPreReport: check Client File Name not blank.

    - OnPostReport: process at server side, download to client, then open client file or not.

    --------------------------------------------------

    OBJECT Report 50001 Test Save File

    {

     OBJECT-PROPERTIES

     {

       Date=10/25/14;

       Time=[ 6:57:57 AM];

       Modified=Yes;

       Version List=;

     }

     PROPERTIES

     {

       OnPreReport=BEGIN

                     //Check input client file name

                     IF (gTxt_ClientFileName = '') THEN

                      ERROR('Please specify Client File Name!');

                   END;

       OnPostReport=BEGIN

                      //Server side: process data with temporary file

                      gTxt_ServerFileName := gCoU_FileMgmt.ServerTempFileName('txt');

                      gFil_File.CREATE(gTxt_ServerFileName);

                      gFil_File.TEXTMODE(TRUE);

                      gFil_File.WRITE('Test 1');

                      gFil_File.WRITE('Test 2');

                      gFil_File.CLOSE;

                      //Download file from Server to Client

                      gCoU_FileMgmt.DownloadToFile(gTxt_ServerFileName, gTxt_ClientFileName);

                      //Open file after download

                      IF gBol_OpenAfterDownload THEN

                        HYPERLINK(gTxt_ClientFileName);

                    END;

     }

     DATASET

     {

     }

     REQUESTPAGE

     {

       PROPERTIES

       {

         SaveValues=Yes;

       }

       CONTROLS

       {

         { 1   ;    ;Container ;

                     Name=ContentArea;

                     ContainerType=ContentArea }

         { 2   ;1   ;Group     ;

                     Name=Client File;

                     GroupType=Group }

         { 3   ;2   ;Field     ;

                     Name=File Name;

                     SourceExpr=gTxt_ClientFileName;

                     OnAssistEdit=BEGIN

                                    gTxt_ClientFileName := gCoU_FileMgmt.SaveFileDialog('Save As', 'Output File.txt', 'Text File (*.txt)|*.txt');

                                  END;

                                   }

         { 4   ;2   ;Field     ;

                     Name=Open After Download;

                     SourceExpr=gBol_OpenAfterDownload }

       }

     }

     LABELS

     {

     }

     CODE

     {

       VAR

         gTxt_ClientFileName@1000 : Text;

         gTxt_ServerFileName@1001 : Text;

         gCoU_FileMgmt@1002 : Codeunit 419;

         gFil_File@1003 : File;

         gBol_OpenAfterDownload@1004 : Boolean;

       BEGIN

       END.

     }

    }

  • Verified answer
    keoma Profile Picture
    keoma 32,675 on at
    RE: Save .txt File

    hi,

    try simple version:

    in report - OnPostReport()

    ------------------------------------------------------

    FileName := FileManagement.SaveFileDialog('Save as','file.txt','Text files (*.txt)|*.txt');

    IF FileName = '' THEN

     ERROR('No filename given.');

    SrvFileName := 'c:\temp\srv-file.txt';

    File.CREATE(SrvFileName);

    File.CREATEOUTSTREAM(outStream);

    outStream.WRITETEXT('line 1');

    outStream.WRITETEXT;

    outStream.WRITETEXT('line 2');

    outStream.WRITETEXT;

    File.CLOSE;

    FileManagement.DownloadToFile(SrvFileName,FileName);

  • Vanessa Shin Profile Picture
    Vanessa Shin 2,558 on at
    RE: Save .txt File

    Hi,

    May I know which section I should put? I had write the code as you mentioned but still cannot. Am I locate in wrong section?

    1) Client side

    error2.png

    2) Server side

    error3.png

    3) Download from server to client

    Please advise.

  • Suggested answer
    4BzSoftware Profile Picture
    4BzSoftware 6,071 on at
    RE: Save .txt File

    Hi Vanessa,

    Take a look at my Query and Excel Report Tool code on NAV 2013:

    //1. Client side: user specifies file name or cancel

    lTxt_ClientFileName := gCoU_FileMgmt.SaveFileDialog('[4Bz] Download Excel Template File', 'Navision Query and Excel Template', 'Excel File (*.xlsx)|*.xlsx');

    IF (lTxt_ClientFileName = '') THEN

     EXIT;

    //2. Server side: process data with temporary file

    lTxt_ServerFileName := gCoU_FileMgmt.ServerTempFileName('xlsx');

    ////Your code here////

    //3. Download file from Server to Client

    gCoU_FileMgmt.DownloadToFile(lTxt_ServerFileName, lTxt_ClientFileName);

  • Vanessa Shin Profile Picture
    Vanessa Shin 2,558 on at
    RE: Save .txt File

    Hi,

    Yes, I had choose the file location. Actually I wish to export the Total Amount of VAT Statement report into text file. Code like:-

    File1.CREATE(FileName);

    File1.CREATEOUTSTREAM(OutStreamObj);

    OutStreamObj.WRITETEXT (FORMAT(TotalAmount, 0, '<Precision,2:2><Standard Format,1>'));

    OutStreamObj.WRITETEXT ('|');

    File1.CLOSE;

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans