How to Fax Nav Reports from Nav

ara3nara3n Posts: 9,144Member
edited 2014-10-13 in NAV Tips & Tricks
Hello I've recently posted a blog on how to fax from Nav. Hopefully people find it useful.

Here is the link for detail explanation.
http://mibuso.com/blogs/ara3n/2009/06/1 ... amics-nav/
IF ISCLEAR(FaxServer) THEN
  CREATE(FaxServer);

IF ISCLEAR(FaxDoc) THEN
  CREATE(FaxDoc);

FaxServer.Connect('');

FileName :=  'C:\test.rtf';
REPORT.SAVEASHTML(REPORT::"Top __ Customer List", FileName ,TRUE);
FaxDoc.Body := FileName;
FaxDoc.DocumentName := 'My  Fax';
FaxDoc.Recipients.Add('17342351901');  //Fax Number
FaxDoc.ConnectedSubmit(FaxServer);


And the variables are.

Name DataType Subtype Length
FaxServer Automation 'Microsoft Fax Service Extended COM Type Library'.FaxServer
FaxDoc Automation 'Microsoft Fax Service Extended COM Type Library'.FaxDocument
JobID Text 30
FileName Text 100

Comments

  • garakgarak Posts: 3,301Member
    yes michelle, it's very easy.

    Here a LINK to the MSDN to learn more about MS Fax Service and to learn how to use it.

    Regards
    Do you make it right, it works too!
  • ara3nara3n Posts: 9,144Member
    I tired it, it worked.
    Great work.Thanks

    You are welcome.
  • atankersleyatankersley Posts: 32Member
    Thanks for the post. Great job.

    The code works great. I use the code you posted for the PDFCreater and this code to fax invoices in a batch. I am just having one problem. When I do the batch the faxserver is opening up a adobe reader window for every fax and when it gets to about 30 windows it crashes.

    Any ideas?
    tank
  • ara3nara3n Posts: 9,144Member
    I would use process of elimination to find out the cause.
    First thing is to rule out adobe.

    Instead of faxing pdf. try to fax a text file and see that you can batch fax 50 times.
    If it is adobe, try to save the report as html. or get another software that opends pdf documents.
  • atankersleyatankersley Posts: 32Member
    I was able to send 100+ faxes. It took the program about 15 mins to load the fax server and all were sent in about 3 hours. Here is what I came up with. The OnRun() trigger for this codeunit calls a report five times that will only process 25 invoices at a time. The report calls the SendInvoiceFax() method for each invoice, flags the invoice headers as sent, and print the sent invoice information. Using the SHELL(taskkill) to kill adobe lets the batch keep running. Using it to kill pdfcreator allows the batch to keep running even if one of the invoices has a problem and doesn't create. I hope this might help someone.

    Thanks again Rashed for all of your help,

    OnRun()
    CLEAR(FaxSalesInvoices);
    FaxSalesInvoices.USEREQUESTFORM(FALSE);
    FaxSalesInvoices.SetProperties('..' + FORMAT(WORKDATE));
    FaxSalesInvoices.RUN;
    CLEAR(FaxSalesInvoices);
    SHELL('taskkill /F /IM AcroRd32.exe /T');
    COMMIT;
    
    CLEAR(FaxSalesInvoices);
    FaxSalesInvoices.USEREQUESTFORM(FALSE);
    FaxSalesInvoices.SetProperties('..' + FORMAT(WORKDATE));
    FaxSalesInvoices.RUN;
    CLEAR(FaxSalesInvoices);
    SHELL('taskkill /F /IM AcroRd32.exe /T');
    COMMIT;
    
    CLEAR(FaxSalesInvoices);
    FaxSalesInvoices.USEREQUESTFORM(FALSE);
    FaxSalesInvoices.SetProperties('..' + FORMAT(WORKDATE));
    FaxSalesInvoices.RUN;
    CLEAR(FaxSalesInvoices);
    SHELL('taskkill /F /IM AcroRd32.exe /T');
    COMMIT;
    
    CLEAR(FaxSalesInvoices);
    FaxSalesInvoices.USEREQUESTFORM(FALSE);
    FaxSalesInvoices.SetProperties('..' + FORMAT(WORKDATE));
    FaxSalesInvoices.RUN;
    CLEAR(FaxSalesInvoices);
    SHELL('taskkill /F /IM AcroRd32.exe /T');
    COMMIT;
    
    CLEAR(FaxSalesInvoices);
    FaxSalesInvoices.USEREQUESTFORM(FALSE);
    FaxSalesInvoices.SetProperties('..' + FORMAT(WORKDATE));
    FaxSalesInvoices.RUN;
    CLEAR(FaxSalesInvoices);
    SHELL('taskkill /F /IM AcroRd32.exe /T');
    
    MESSAGE('ExpandIT Launch Utility Finished');
    
    SendInvoiceFax(VAR SalesInvoiceHeader : Record "Sales Invoice Header";ReportID : Code[10]) FaxWasSent : Text[30]
    CLEARALL;
    
    FaxWasSent := 'Yes';
    
    IF Customer.GET(SalesInvoiceHeader."Bill-to Customer No.") THEN
       BEGIN
          TempSting := '86' + DELCHR(Customer."A/R Fax No.",'=','-/()');
          PhoneNo := DELCHR(TempSting,'=',DELCHR(TempSting,'=','1234567890')); //PhoneNo contains only numbers
       END
    ELSE
       PhoneNo := '';
    
    IF (PhoneNo = '') OR (STRLEN(PhoneNo) <> 12) THEN
       BEGIN
          MESSAGE('ERROR Invalid Fax Number ' + FORMAT(SalesInvoiceHeader."No."));
          SHELL('taskkill /F /IM AcroRd32.exe /T');
          SHELL('taskkill /F /IM PDFCreator.exe /T');
          EXIT('No / Invalid Fax No.');
       END;
    
    IF ReportID = '' THEN
       ReportNo := 50138   //Sales Invoice 2
    ELSE
       EVALUATE(ReportNo,ReportID);
    
    FileDirectory := 'E:';
    FileName := FORMAT(SalesInvoiceHeader."No.") + 'fax';
    FileName2 := FORMAT(SalesInvoiceHeader."No.") + 'fax.pdf';
    
    IF ISCLEAR(PDFCreator) THEN 
      CREATE(PDFCreator); 
    IF ISCLEAR(PDFCreatorError) THEN 
      CREATE(PDFCreatorError); 
     
    PDFCreatorError := PDFCreator.cError; 
    PDFCreator.cIsConverted := FALSE;
    IF PDFCreator.cStart('/NoProcessingAtStartup',TRUE) = FALSE THEN
       BEGIN
          MESSAGE('PDFCreator ' + FORMAT(SalesInvoiceHeader."No."));
          SHELL('taskkill /F /IM AcroRd32.exe /T');
          SHELL('taskkill /F /IM PDFCreator.exe /T');
          EXIT('No / PDFCreater Error');
       END;
    
    Object.GET(Object.Type::Report,'',ReportNo);
    
    PDFCreatorOption :=  PDFCreator.cOptions; 
    
    PDFCreatorOption.UseAutosave := 1;
    PDFCreatorOption.UseAutosaveDirectory := 1;
    PDFCreatorOption.AutosaveDirectory := FileDirectory; 
    PDFCreatorOption.AutosaveFormat := 0;         
    PDFCreatorOption.AutosaveFilename := FileName;
    
    PDFCreator.cOptions := PDFCreatorOption; 
    PDFCreator.cClearCache(); 
    DefaultPrinter := PDFCreator.cDefaultPrinter; 
    PDFCreator.cDefaultPrinter := 'PDFCreator'; 
    PDFCreator.cPrinterStop := FALSE; 
    
    REPORT.RUNMODAL(ReportNo,FALSE,FALSE,SalesInvoiceHeader);
    
    TimeOut := 0;
    REPEAT
       SLEEP(1000);
       TimeOut := TimeOut + 1;
    UNTIL (PDFCreator.cIsConverted) OR (TimeOut = 25);
    
    IF TimeOut = 25 THEN
       BEGIN
          MESSAGE('Time Out Error ' + FORMAT(SalesInvoiceHeader."No."));
          PDFCreator.cPrinterStop := TRUE;
          PDFCreator.cDefaultPrinter := DefaultPrinter;
          SHELL('taskkill /F /IM AcroRd32.exe /T');
          SHELL('taskkill /F /IM PDFCreator.exe /T');
          EXIT('No / Time Out Error');
       END;
    
    PDFCreator.cPrinterStop := TRUE;
    PDFCreator.cDefaultPrinter := DefaultPrinter;
    PDFCreator.cClose;
    
    
    
    IF ISCLEAR(FaxServer) THEN
      CREATE(FaxServer);
    
    IF ISCLEAR(FaxDoc) THEN
      CREATE(FaxDoc);
    
    FileName := FileDirectory + '\' + FileName2;
    FaxDoc.Subject := SalesInvoiceHeader."No.";
    FaxDoc.Body := FileName;
    FaxDoc.DocumentName := SalesInvoiceHeader."Bill-to Customer No." + ' / ' + SalesInvoiceHeader.Name;
    FaxDoc.Recipients.Add(PhoneNo);  //Fax Number
    FaxServer.Connect('');
    FaxDoc.ConnectedSubmit(FaxServer);
    
    IF NOT EXISTS(FileName) THEN
       BEGIN
          MESSAGE('File Not Created Error ' + FORMAT(SalesInvoiceHeader."No."));
          SHELL('taskkill /F /IM AcroRd32.exe /T');
          SHELL('taskkill /F /IM PDFCreator.exe /T');
          EXIT('No / PDF File Not Created');
       END
    ELSE
       ERASE(FileName);
    
    CLEARALL;
    
    
    tank
  • ara3nara3n Posts: 9,144Member
    You are welcome and thanks for sharing the code. And your experience.
    I'm sure other people will find it useful.
  • nightrodnightrod Posts: 87Member
    Hi,

    Using your sample form i receive the following error:

    The Call to member ConnectedSubmit Failed. FaxComEx.FaxDocument.1 returned thefollowing message: Operation Failed.

    Does anyone know what this can be?

    :-k
  • ara3nara3n Posts: 9,144Member
    Is your faxservice running?

    Try to manually send a fax.
  • nightrodnightrod Posts: 87Member
    the faxserver is running and manually it works.
  • ara3nara3n Posts: 9,144Member
    I don't know why you won't be able to connect to the server.

    Try it on another computer.
  • tcrushtcrush Posts: 1Member
    ara3n wrote:
    I would use process of elimination to find out the cause.
    First thing is to rule out adobe.

    Instead of faxing pdf. try to fax a text file and see that you can batch fax 50 times.
    If it is adobe, try to save the report as html. or get another software that opends pdf documents.

    Why would you rule out adobe? What kind of problems does it create?
    Sorry I'm just learning about this stuff, and trying to get a better grasp on it.
    Are there any other links on MS Fax Service that could help me learn more?
    I've gotten a lot of help from Premium Corporate Internet Fax and Inexpensive Online Fax, so hope these links will be of help.
    Thanks!
    Taylor
  • pburgovpburgov Posts: 8Member
    Does anybody know how to open the console/interface of the fax service while sending the fax? I don't want just to send the fax, i want to check manually if it is right and be able to see the preview.
    Thanks
  • pburgovpburgov Posts: 8Member
    It would be fine too if I could save the fax as a draft.
    Thanks.
  • ara3nara3n Posts: 9,144Member
    What do you mean if it's right?
    Why would it be wrong?
  • pburgovpburgov Posts: 8Member
    What I mean Is that among the chosen invoices a wrong one could be, so I could see the preview of the faxes before they were sent I would be great for me. If manually I open the fax console => CLick NEW FAX => Then I can Save a copy in the draft folder to be sent later. When It's needed. Can I do the same thing with these faxes created by code?
    Thanks.
  • ara3nara3n Posts: 9,144Member
    well you can code in NAV to see the Invoices before sending them to fax. Preview in NAV and then send the list of invoices to fax.
  • pburgovpburgov Posts: 8Member
    Yes I know, but there's no need to do it with a new code since I have the preview of the chosen Invoices in the NAV Standard. Using the code you shared, I created a new Button (FAX) in the form 143. So when I choose the invoices to be sent, either by a filter or just clicking on with the mouse one by one, I can see the preview and then run the Fax Code. What I was looking for besides checking the faxes are correct, to modify (if needed) some of the fields of the fax as subject or note.
    Never mind, I assume that can't be done. Thanks anyway, I learn a lot from you.
  • nowbienowbie Posts: 1Member
    HI ara3n,

    I have quit New in FAX coding .

    May i know your this I FAX coding does need match or specific to which window fax version?

    Thanks and regards
Sign In or Register to comment.