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.
0
Answers
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;
I already that but the error still exist.
Maybe you can post the full code as well.
Perhaps, this line returns false and you don't create a file:
Where is FIND or GET of recDirectoryTable? Do you build correct file name?
I don't know what the problem could be but here is an example, like yours, that works.
Regards
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.