Problem:Send a pdf file attachment

foronavisionforonavision Member Posts: 79
I have a report than i want to send by e-mail after convert it in pdf file. I have the following code in onPostReport


IF GeneradoAutomatico THEN BEGIN
CLEAR (PDFCreatorOption);

FileDirectory := Text024;
FileName := Text025 + FORMAT(FechaRecuento) + '.pdf';

IF ISCLEAR(PDFCreator) THEN
CREATE(PDFCreator);
IF ISCLEAR(PDFCreatorError) THEN
CREATE(PDFCreatorError);

PDFCreatorError := PDFCreator.cError;

IF PDFCreator.cStart('/NoProcessingAtStartup',TRUE) = FALSE THEN
ERROR('Status: Error: ' + PDFCreatorError.Description);

IF ISCLEAR(PDFCreatorOption) THEN
CREATE(PDFCreatorOption);
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;

Espera := 0;
REPEAT
Espera += 1;
UNTIL PDFfile.OPEN(FileDirectory + '\' + CONVERTSTR(FileName,'/','_'));

FicheroExistVinos := FileDirectory + '\' + CONVERTSTR(FileName,'/','_');
mail.NewMessage(DirEMailTo,DirEMailCC,Asunto, '',FicheroExistVinos,FALSE);
END;

The pdf file isn't attachmented to the e-mail because isn't generated. Could somebody say me which can be the problem?

Thanks a lot!

Comments

  • kinekine Member Posts: 12,562
    You have the loop for waiting until the file is created, but you have not set the WRITEMODE for the file. It means that the OPEN will succeed when the file is created, but it doesn't mean that the file is released and closed with the PDFCreator. You need to set the file variable into write mode, to try open the file for writing. This will succeed only if the file is released by PDFCreator...
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • foronavisionforonavision Member Posts: 79
    how can i get it?
  • kinekine Member Posts: 12,562
    Espera := 0;
    PDFFile.WRITEMODE := true; //I want to write access to the file...
    REPEAT
    Espera += 1;
    UNTIL PDFfile.OPEN(FileDirectory + '\' + CONVERTSTR(FileName,'/','_'));
    
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • foronavisionforonavision Member Posts: 79
    I try with this code and now the e-mail send but without the pdf file attachment, but this file exist in the directory. ](*,) I dont't know what i do
  • kinekine Member Posts: 12,562
    Do not forget to close the file after you successfully opened it... ;-)
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • foronavisionforonavision Member Posts: 79
    when i execute the report continue in the loop

    Espera := 0;
    PDFfile.WRITEMODE := TRUE;
    REPEAT
    Espera += 1;
    UNTIL PDFfile.OPEN(FileDirectory + '\' + CONVERTSTR(FileName,'/','_'));
    PDFfile.CLOSE;


    and don't exit. I run the report from the job scheduler, could be this the problem?
  • kinekine Member Posts: 12,562
    when i execute the report continue in the loop

    Espera := 0;
    PDFfile.WRITEMODE := TRUE;
    REPEAT
    Espera += 1;
    UNTIL PDFfile.OPEN(FileDirectory + '\' + CONVERTSTR(FileName,'/','_'));
    PDFfile.CLOSE;


    and don't exit. I run the report from the job scheduler, could be this the problem?

    1) add condition to end when the Espera is greater than some max (and add some delay into the loop to not overstress the CPU - e.g. SLEEP(500)
    2) If it never end it means that the file was not created or was created under different name or folder. Check if the PDF was created, if the name is correct and if the folder is correct...
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • NagiNagi Member Posts: 151
    I had a customer who wanted to send multiple pdf files as e-mail attachments. I used PDFCreator, but could never figure out how to loop the codeunit until the event PDFCreator::eReady() fired. However, I solved it in another way. Maybe not the most elegant of solutions, but so far it's been working without any problems.
      IF SalesHeader."REP Save as PDF" THEN BEGIN
        FileCreated := FALSE;
        SetSnoozeTime();
        WHILE NOT FileCreated DO BEGIN
          FileCreated := PrintPDF(ReportID,FileName);
          IncrSnoozeTime();
        END;
    

    SetSnoozeTime resets integer variable to 3000, while IncrSnoozeTime increments the integer value by 1000.

    And this is the function PrintPDF;
    IF ISCLEAR(PDFCreatorError) THEN
      CREATE(PDFCreatorError);
    
    PDFCreatorError := PDFCreator.cError;
    
    IF PDFCreator.cStart('/NoProcessingAtStartup',TRUE) = FALSE THEN
      ERROR('Status: Error[' + FORMAT(PDFCreatorError.Number) + ']: ' + PDFCreatorError.Description);
      
    PDFCreatorOption :=  PDFCreator.cOptions;
    PDFCreatorOption.UseAutosave := 1;
    PDFCreatorOption.UseAutosaveDirectory := 1;
    PDFCreatorOption.AutosaveDirectory := SalesSetup."Path E-Mail Attachments";
    PDFCreatorOption.AutosaveFormat := 0;  //PDF file, you can also save in other formats
    PDFCreatorOption.AutosaveFilename := FileName;
    PDFCreator.cOptions := PDFCreatorOption;
    PDFCreator.cClearCache();
    DefaultPrinter := PDFCreator.cDefaultPrinter;
    PDFCreator.cDefaultPrinter := 'PDFCreator';
    PDFCreator.cPrinterStop := FALSE;
    REPORT.RUNMODAL(ReportID,FALSE,TRUE,SalesHeader2);
    
    SLEEP(SnoozeTime);
    
    PDFCreator.cPrinterStop := TRUE;
    PDFCreator.cDefaultPrinter := DefaultPrinter;
    PDFCreator.cClose();
    
    IF EXISTS(SalesSetup."Path E-Mail Attachments" + FileName) THEN
      EXIT(TRUE)
    ELSE
      EXIT(FALSE);
    

    I would welcome comments on my code, so that I might improve it. As I said it works fine and all the pdf attachments are generated, but it might not be the most time efficient way of doing things...

    The last thing I did was to modify the codeunit Mail, so that it could send multiple attachments;
    SalesSetup.GET;
    SalesSetup.TESTFIELD("Path E-Mail Attachments");
    
    recFile.SETRANGE(Path,'C:\');
    IF recFile.FINDFIRST THEN
      ;  //Do nothing.
    
    recFile.SETRANGE(Path,SalesSetup."Path E-Mail Attachments");
    recFile.SETRANGE("Is a file",TRUE);
    IF recFile.FINDSET THEN
      REPEAT
        BSTRConverterAttachFileName.ResetBSTR;
        BSTRConverterAttachFileName.AppendNextStringPortion(recFile.Path + recFile.Name);
        OAttachments := OSendMail.Attachments;
        OAttachment := OAttachments.Add(BSTRConverterAttachFileName);
        ERASE(recFile.Path + recFile.Name);
      UNTIL recFile.NEXT = 0;
    
    OSendMail.OpenDialog := OpenDialog;
    
    MailSent := OSendMail.Send;
    ErrorNo := OSendMail.ErrorStatus;
    OApplication.Logoff;
    
  • hs_mannhs_mann Member Posts: 17
    I have just finished implementing PDFCreator Version 0.9.5 on VISTA Business.

    Try using

    REPEAT
    UNTIL PDFCreator.cIsConverted;


    Instead of this
    Espera := 0;
    REPEAT
    Espera += 1;
    UNTIL PDFfile.OPEN(FileDirectory + '\' + CONVERTSTR(FileName,'/','_'));

    I found this function in PDFCreator Version 0.9.5
Sign In or Register to comment.