The File is not Open Error

CHADELCHADEL Member Posts: 16
edited 2019-08-09 in NAV Three Tier
Hello. I am trying to generate a csv file using Dynamics NAV report. But I'm getting a "The file is not open" error whenever I try to create the file. This is currently what I've done:

OnPreDataItem()
varFileName := recDirectoryTable."Sample Directory" + '\' + 'SampleCSVFile.csv';

IF varFile.CREATE(varFileName) THEN BEGIN
varFile.TEXTMODE(TRUE);
// header -
varFile.WRITE()
...
// header +
END;

OnAfterGetRecord()
// body -
varFile.WRITE()
...
// body +

OnPostDataItem()
varFile.CLOSE;

MESSAGE('File creation done!');

Is there something missing here? I've also checked forums but unfortunately didn't get the answer I want. Thank you.

Best Answer

  • CHADELCHADEL Member Posts: 16
    edited 2019-08-13 Answer ✓
    Hello folks.
    Thank you for all the inputs. It finally worked.
    What I did is I changed the directory to 'D:\'
    ex. varFileName := 'D:\SampleCSVFile.csv';
    and the file was created (which is what I saw from @ftornero). I also tried using a table for the directory
    ex. varFileName := recDirectoryTable."Sample Directory" + '\' + 'SampleCSVFile.csv';
    and the file was created. What's missing in my first code is the FIND or GET of recDirectoryTable, my bad. Thanks @AlexDen.
    What's weird is that when I changed the directory to 'C:\', the same error appears. Which is still a problem because I want the files to be saved in Documents.

Answers

  • AluanAluan Member Posts: 164
    edited 2019-08-09
    Hello CHADEL,

    you have to open the File before u write to it.

    Example from the Documentation (https://docs.microsoft.com/en-us/dynamics-nav/textmode-function--file-):
    TestFile.TEXTMODE(TRUE);
    TestFile.WRITEMODE(TRUE);
    TestFile.OPEN('C:\TestFolder\TestFile.txt');
    TestFile.WRITE('Hello World');
    TestFile.CLOSE;
  • CHADELCHADEL Member Posts: 16
    Aluan.
    I already that but the error still exist.
  • AluanAluan Member Posts: 164
    Where exactly do you get the error? Can you debug it?
    Maybe you can post the full code as well.
  • AlexDenAlexDen Member Posts: 86
    Hi,

    Perhaps, this line returns false and you don't create a file:
    IF varFile.CREATE(varFileName) THEN BEGIN
    

    Where is FIND or GET of recDirectoryTable? Do you build correct file name?
    varFileName := recDirectoryTable."Sample Directory" + '\' + 'SampleCSVFile.csv';
    

  • ftorneroftornero Member Posts: 524
    Hello @CHADEL ,

    I don't know what the problem could be but here is an example, like yours, that works.
    OBJECT Report 50012 Write File Test
    {
      OBJECT-PROPERTIES
      {
        Date=09/08/19;
        Time=12:00:18;
        Modified=Yes;
        Version List=;
      }
      PROPERTIES
      {
        ProcessingOnly=Yes;
      }
      DATASET
      {
        { 1000000000;;DataItem;                  ;
                   DataItemTable=Table2000000026;
                   DataItemTableView=SORTING(Number)
                                     WHERE(Number=FILTER(1..10));
                   OnPreDataItem=BEGIN
                                   varFileName := 'D:\Paso\salida.txt';
                                   IF varFile.CREATE(varFileName) THEN BEGIN
                                     varFile.TEXTMODE(TRUE);
                                     // header -
                                     varFile.WRITE('Header');
                                     // header +
                                   END;
                                 END;
    
                   OnAfterGetRecord=BEGIN
                                      varFile.WRITE('Line '+ FORMAT(Integer.Number));
                                    END;
    
                   OnPostDataItem=BEGIN
                                    varFile.CLOSE;
                                    MESSAGE('File creation done!');
                                  END;
                                   }
    
      }
      REQUESTPAGE
      {
        PROPERTIES
        {
        }
        CONTROLS
        {
        }
      }
      LABELS
      {
      }
      CODE
      {
        VAR
          varFile@1000000000 : File;
          varFileName@1000000001 : Text;
    
        BEGIN
        END.
      }
      RDLDATA
      {
      }
    }
    

    Regards
  • TallyHoTallyHo Member Posts: 417
    edited 2019-08-09
    If you use a static file name, my guess is that you have the file open in notepad or Excel. Try closing everything but nav. And remove the IF before create. You want an error if f.i. the path is not correct. In that case the error even makes sense.
  • CHADELCHADEL Member Posts: 16
    edited 2019-08-13 Answer ✓
    Hello folks.
    Thank you for all the inputs. It finally worked.
    What I did is I changed the directory to 'D:\'
    ex. varFileName := 'D:\SampleCSVFile.csv';
    and the file was created (which is what I saw from @ftornero). I also tried using a table for the directory
    ex. varFileName := recDirectoryTable."Sample Directory" + '\' + 'SampleCSVFile.csv';
    and the file was created. What's missing in my first code is the FIND or GET of recDirectoryTable, my bad. Thanks @AlexDen.
    What's weird is that when I changed the directory to 'C:\', the same error appears. Which is still a problem because I want the files to be saved in Documents.
Sign In or Register to comment.