Error message for NAV e-mail attachment

gadzilla1gadzilla1 Member Posts: 316
Hello All,

I've recently implemented some code which allows a user to save a report as a PDF and attach to an e-mail.

However, I believe I am getting an error due to the '.pdf' extension not being read.

The errors I am recieving are occurring on the sales order after clicking Function and Email Confirmation. The errors are 'Send Mail Error Number = 32011' and then 'The operating system cannot find the file c:\se_dem0\temp\Sales Invoice'

In the bold area above, I believe the system is looking for Sales Invoice and should be looking for Sales Invoice.pdf.

Has anyone had this same issue? Any tips, suggestions are greatly appreciated!

Thanks and have a great day. gad1

Comments

  • XypherXypher Member Posts: 297
    Show us some code :shock:
  • ara3nara3n Member Posts: 9,256
    Xypher wrote:
    Show us some code :shock:

    lol I was about to reply the same thing and did a refresh and saw your post.
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • gadzilla1gadzilla1 Member Posts: 316
    Why the shock face? lol - I can't take credit for the code, a peer found it online and I used it and it seems to work to the point I encountered errors above. You've probably seen it already, it uses PDF Creator to generate a PDF attachment.
  • ReinhardReinhard Member Posts: 249
    gadzilla I have been doing something similar, with the same code.
    When they asked for code, maybe post the code behind 'Email Notification'

    I have two pieces of advice:
    It may be easier to show people that when they select PDF creator as teh printer on a report, there is already an option to Email! No reason to try to code it up yourself maybe...

    Second, the PDFCreator takes a little bit of TIME to actually create the pdf. So try putting in
    IF CONFIRM('send email?') THEN ;
    

    right before you try to open the file. This way the system will pause until you press 'Yes' and give the pdf time to be generated. If this turns out to be the problem and you want more help to solve it I have code.
  • kinekine Member Posts: 12,562
    Or you can wait until the file is generated by trying to open the file for writing. If you fail, wait. If you succeeded, all is OK and you can e-mail the file... ;-)
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • EugeneEugene Member Posts: 309
    PDFCreator supports automation 'PDFCreator'.clsPDFCreator

    this automation object has events that can report back to NAvision if the pdf file was successfully created - define trigger PDFCreator::eReady()


    your problem is most likely cause by truncation of filename (what is the length of a variable that holds filename ?)
  • DenSterDenSter Member Posts: 8,305
    Don't just add that trigger though, declare the PDFCreator as 'withevents' and it should show up automatically.
  • kinekine Member Posts: 12,562
    Yes, but for me the trigger is not working at 100% and I needed to use another way how to detect this. It is mainly because if the event is fired when NAV is still busy, the event is not started in NAV...
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • XypherXypher Member Posts: 297
    gadzilla1 wrote:
    Why the shock face? lol - I can't take credit for the code, a peer found it online and I used it and it seems to work to the point I encountered errors above. You've probably seen it already, it uses PDF Creator to generate a PDF attachment.

    The shock face was more or less to show my eyes open wide waiting for the code post :mrgreen:


    What I can suggest is to create a 'WaitForFile' function either using SLEEP or Navision Timer 1.0 (timer is better because a sleep loop can make what appears to be an infinite loop [short lockup])
    WaitForFile(strFilePath : Text[1024];intSeconds : Integer) FoundIt : Boolean
      FOR i := 1 TO intSeconds DO BEGIN
        IF EXISTS(strFilePath) THEN BEGIN
          FoundIt := TRUE;
          EXIT;
        END;
        SLEEP(1000);
      END;
    

    This is the layout of a function I use, but.. it may not work exactly for what you need. As kine said...
    kine wrote:
    ...Or you can wait until the file is generated by trying to open the file for writing. If you fail, wait...

    (It is different because the file may exist and also still may be in use; and access will be denied to read from the file to send as attachment.)
  • ReinhardReinhard Member Posts: 249
    wow I struggled for days with this problem... now here are many good answers.
    Xypher I have a variation on your code... I like the idea of your method.
    WaitForFile(strFilePath : Text[1024];intSeconds : Integer) FoundIt : Boolean
      start := TIME; 
      WHILE (TIME - intSeconds*1000) < start DO 
        IF EXISTS(strFilePath) THEN 
          EXIT(TRUE);  
    // if we get here then file was not found in allowed time
    EXIT(FALSE);
    

    no need for SLEEP or Navision Timer

    can someone give a little more detail on how to use eReady trigger?
  • XypherXypher Member Posts: 297
    Reinhard wrote:
    can someone give a little more detail on how to use eReady trigger?

    Open the Global/Local variables in the function you use PDFCreator, select the Automation that has 'PDFCreator'.clsPDFCreator, hit Shift-F4 to open the properties page, and finally set WithEvents = Yes. After this Navision will automatically create the event functions provided through the automation.

    (Events will always be seen at the very bottom of the code page.)


    Basically eReady will trigger after a print process has been completed (and you usually want to clean up the process and revert back to the system's default printer.)
  • ReinhardReinhard Member Posts: 249
    right...

    so a good way to use the trigger for this problem might be to have a boolean printerReady, right before you start your print process set it to false. Then in eReady set it to true. My problem is that I was printing multiple reports, but only ending up with the last one... the code was calling the printer before it had time to finish/close the last report.
    ie
    WHILE reportsLeftToPrint DO
      IF printerReady THEN BEGIN
        printerReady := FALSE;
        // generate PDF...
        // blah blah
      END;
    
  • XypherXypher Member Posts: 297
    How do you go about running the PDFCreator function? Like what parameters are you using?

    Reason why I ask is.. what is the filepath/name PDFCreator writing to each time? Do you change per Report? Otherwise you're just overwring the same file over and over.
  • ReinhardReinhard Member Posts: 249
    yeah no worries they have different file names... it works well now. Just for a while I was stumped... which is why I'm excited to see this thread so that no one else has to suffer
  • kinekine Member Posts: 12,562
    Biggest problem of the event is, that it is called asynchronously and it means, if you have process like "print-attach-send" in one "transaction", you cannot use it, because if there will be some loop or sleep to wait for the trigger, the trigger will be never fired, because NAV must be in "idle state" - doing nothing. And it is hard to do that. I have used to open modal form from my code, and using timer on it testing some flag which will be set in the event. ut sometime the form was open and never closed. It is why I am using right now something like:
     File.WriteMode := True;
      while not File.Open(PdfFile) then
        sleep(SleepTime);
      File.Close;
      //Now the file was created and is free for attaching it to the mail...
    

    You can add some counter to prevent never-ending loop in case of some problems with the PdfPrinter and you are done...[/code]
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • hs_mannhs_mann Member Posts: 17
    Try using

    REPEAT
    UNTIL PDFCreator.cIsConverted;

    after REPORT.RUNMODAL

    Tested on PDFCreator Version 0.9.5

    Harjot
Sign In or Register to comment.