Options

Export a Text File in NAV 2013

imclever1205imclever1205 Member Posts: 94
edited 2015-07-21 in NAV Three Tier
Hi Guys ,
Does anyone knows the correct code to exectute and Xmlport that needs to write a text file and store it in a location ?

F1 suggested :
VarXml.CREATE('C:\'+"Sales Shipment Header". "No." + '.txt');
VarXml.CREATEOUTSTREAM(OutputStream);

XMLPORT.EXPORT(6059810, OutputStream );
VarXml.CLOSE;

But it does not work actually : Microsoft Dynamics NAV

The following error occurred: Could not find a part of the path 'C:\102001.txt'.

I have written a code which uses codeunit File Management and it works fine
TextFile.CREATE(FilePath);
TextFile.CREATEOUTSTREAM(OutStream);
OutStream.WRITETEXT(Vendor."Mnemonic Code"+';'+FORMAT(PurchLine.Quantity)+';'+PurchLine."No."+';'+PurchLine.Description+';'+"No."+';'+'Y');
TextFile.CLOSE;
FileManagement.DownloadHandler(FilePath,'Test',PurchasePayableSetup."BogPortal Export Path",'(*.txt)|*.txt|All files (*.*)|*.*','');

This code works fine and export the file on the Client Side in the specified folder but the only issue is that it pops up the Save As Window.

And for my current problem I do not want it to pop up the SaveAs window.I want it to store in the path specified in a background process
Can someone please help or suggest a work around ?
Thank you

Comments

  • Options
    yukonyukon Member Posts: 361
    Hi,

    Did you try to use "File Management"."DownloadToFile" func.?

    Regards,
    Yukon
    Make Simple & Easy
  • Options
    imclever1205imclever1205 Member Posts: 94
    Yes i tried this one too and gives the same error and best there are no examples that shows the correct way to do it
  • Options
    it_wokwit_wokw Member Posts: 1
    Hi,
    I've spent some time on mibuso to find a solution for similar case but I couldn't find proper way for "silent" (without Save As window) export data for filtered record.
    Based on many tries I built this piece of code. It works for me, so I hope it will be useful.


    Fil_ServerFileName := FileMgt.ServerTempFileName('csv');
    File2.CREATE(Fil_ServerFileName);
    File2.CREATEOUTSTREAM(OutStream);
    xmlportname.SETDESTINATION(OutStream);
    xmlportname.SETTABLEVIEW(filteredrecord);
    xmlportname.EXPORT();
    File2.CLOSE();
    gFilename := path + filename;
    FileMgt.DownloadToFile(Fil_ServerFileName,gFilename);
    FILE.ERASE(Fil_ServerFileName);
  • Options
    vaprogvaprog Member Posts: 1,128
    Hi,

    you did not clearly specify whether you need client side file paths or can do it server side. The latter is somewhat easyer and more effective.

    But as you say you client side code work save the prompt, I'll give you a code sample for client side storage.
    ExpFileName contains the full filename (Drive|Server\share,path,name,extension) of the destination file.
    TempBlob.Blob.CREATEOUTSTREAM(OutStrm);
    XMLPORT.EXPORT(XMLPortID,OutStrm);
    CLEAR(OutStrm);
    IF TempBlob.Blob.HASVALUE THEN BEGIN
      BlobFilename := FileMgt.BLOBExport(TempBlob,ExpFilename,FALSE);
      IF FileMgt.ClientFileExists(BlobFilename) THEN BEGIN
        FileMgt.MoveAndRenameClientFile(
          BlobFilename,
          FileMgt.GetFileName(ExpFilename),
          FileMgt.GetDirectoryName(ExpFilename);
      END;
    END;
    CLEAR(TempBlob);
    
    Note that FileMgt.BLOBExport, even though you pass it the destination file name, actually only just keeps the extension part an stores it's export to the user's TEMP folder on the client. From there you can the move it to where you wanted it in the first place. NAV's file management is full of obstacles and unexpected behaviour.

    I wonder about your (imclever1205) first example. I'd never imagined that would work. On which line does it fail? Either it tries to read from the file you attempt to create or it may fail because of UAC (writing to the root of the system drive requires privileged access) and the error message is bogus.
Sign In or Register to comment.