Error: Write to file failed

Aravindh_NavisionAravindh_Navision Member Posts: 258
Hi friends,

[Version: NAV 2009 SP1 Classic client]

I am extracting data from NAV to text file through the automation from report using 'Microsoft ActiveX Data Objects 2.8 Library'.Stream'. When a file with same name already exists in the folder, I am getting below error:

This message is for C/AL programmers.

The call to member SaveToFile failed.ADODB.Stream returned the following message:
Write to file failed.


Can anyone please let me know how to overcome this issue?

Thanks and Regards.

Comments

  • allenyyzhouallenyyzhou Member Posts: 142
    Hi,

    It may caused by several possible reason, one is the file path is not correct.
  • Aravindh_NavisionAravindh_Navision Member Posts: 258
    Thank you allenyyzhou. Any solution..? Do I need to change in coding or in security of the folder?
  • allenyyzhouallenyyzhou Member Posts: 142
    Could you paste all source code related in here?
  • Aravindh_NavisionAravindh_Navision Member Posts: 258
    edited 2017-12-28
    Hi allenyyzhou, heres is the code.

    Global variables

    Name DataType Subtype
    adostream Automation 'Microsoft ActiveX Data Objects 2.8 Library'.Stream
    CRL Char
    LFL Char
    
    <Declared and assigned variables and concatenated as shown below>
    
    CRL := 13;
    LFL := 10;
    
    CREATE(adostream);
    adostream.Open;
    adostream.Charset('UTF-8');
    
    concRowID01 := RowID01 + '|' + Serie01 + '|' + Folio01 + '|' + PostingDateTime01 + '|' + WaytoPay01 +
     '|' + PaymentTerms01 + '|' + txtSubtotal01 + '|' + txtDiscount01 + '|' + CurrCode01 + '|' + txtExchRate01 +'|' + txtTotal01 + '|' + txtTotalWithholdingTax01 + '|' + txtTotalTransferredTax01 + '|' + TypeOfReceipt01 + '|' + PmtMethod01 + '|' + EmissionPlace01 + '|' + AmountInWords01 + '|' + Confirmation01 + '|' + UseCFDI01 +'|' + Relation01;
    
    [b]adostream.WriteText(concRowID01 + FORMAT(CRL) + FORMAT(LFL));[/b]
    
    concRowID03 := RowID03 + '|' + RFC03 + '|' + CompanyName03 + '|' + FiscalRegime03 + '|' + Branch03 +
     '|' + Country03 + '|' + Street03 + '|' + OutDoorNo03 + '|' + InteriorNo03 + '|' + SuburbName03 +
     '|' + StateCity03 + '|' + DelegacionMunicipo03 + '|' + ZipCode03;
    
    [b]adostream.WriteText(concRowID03 + FORMAT(CRL) + FORMAT(LFL));[/b]
    
    concRowID04 := RowID04 + '|' + CustomerNo04 + '|' + RFC04 + '|' + CustomerName04 + '|' + FiscalAddress04 + '|' + TaxID04 + '|' + Country04 + '|' + Street04 + '|' + OutDoorNo04 + '|' + InteriorNo04 + '|' + SuburbName04 + '|' + StateCity04 + '|' + DelegacionMunicipo04 + '|' + ZipCode04 + '|' + Email04;
    
    [b]adostream.WriteText(concRowID04 + FORMAT(CRL) + FORMAT(LFL));[/b]
    
    concRowID05 := RowID05 + '|' + ProSerCode05 + '|' + IDNo05 + '|' + txtQty05 + '|' + IDUnit05 + '|' + 
     UOM05 +'|' + Desc05 + '|' + txtUnitPrice05 + '|' + txtLineAmount05 + '|' + txtLineDiscount05;
    
    [b]adostream.WriteText(concRowID05 + FORMAT(CRL) + FORMAT(LFL));[/b]
       
    concRowID05A := RowID05A + '|' + txtBase05A + '|' + Tax05A + '|' + FactorType05A + '|' + txtTaxRate05A + '|' + txtAmount05A;
    
    [b]adostream.WriteText(concRowID05A + FORMAT(CRL) + FORMAT(LFL));[/b]
    
    concRowID07 := RowID07 + '|' + TaxDescription07 + '|' + FactorType07 + '|' +txtTaxRate07 + '|' + txtAmount07;
    
    [b]adostream.WriteText(concRowID07 + FORMAT(CRL) + FORMAT(LFL));[/b]
    
    SISTring := COPYSTR("Sales Invoice Header"."No.",1,3);
    IF SISTring = 'INT' THEN BEGIN
      MESSAGE('Internal Invoices cannot be extracted to text file for Electronic Invoicing. ' +
              'Check whether you have filtered any internal invoices starting with INT.');
      CurrReport.QUIT;
    END ELSE
      adostream.SaveToFile('D:\TestFile\UTF8\' +
                            FORMAT(WORKDATE,0,'Invoice_' + "No." + '_<Day,2>' + '<Month,2>' + '<Year4>') + '.txt');
    
    adostream.Close;
    


    Please let me know, if you need any more details.

    Thanks.
  • Slawek_GuzekSlawek_Guzek Member Posts: 1,690
    How about
    OutFile := 'D:\TestFile\UTF8\Invoice_' + "No." + '_'+FORMAT(WORKDATE,0,'<Day,2><Month,2><Year4>') + '.txt';
    adostream.SaveToFile(OutFile, 2);
    
    ADO Stream SaveToFile Method :
    SaveToFile FileName, SaveOptions
    
    A SaveOptionsEnum value that specifies whether a new file should be created by SaveToFile, if it does not already exist. Default value is adSaveCreateNotExists. With these options you can specify that an error occurs if the specified file does not exist. You can also specify that SaveToFile overwrites the current contents of an existing file.
    Slawek Guzek
    Dynamics NAV, MS SQL Server, Wherescape RED;
    PRINCE2 Practitioner - License GR657010572SG
    GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
  • Aravindh_NavisionAravindh_Navision Member Posts: 258
    Thanks Slawek, It worked.. :smile:
Sign In or Register to comment.