Word Layout for Email Body

rjsfrjsf Member Posts: 17
Hello, I've been trying to put together some code to send a custom report via email. This email is a "Debit Notification" sent from a custom function to notify customers, so I could not use the built-in email functions. The client requested the format be Word. I used pointers from this blog post on how to do it: https://saurav-nav.blogspot.com/2017/08/microsoft-dynamics-nav-2017-email-from_21.html

My understanding is that the Word Layout is a BLOB that must first be saved as HTML format via REPORT.SAVEASHTML, and then used as the body for the E-mail. I created a function below to generate the HTML, given the record, the Layout Code and the Report ID:

LOCAL GenerateEmailBody(DocumentVariant : Variant;ReportID : Integer;VAR BodyFilePath : Text;LayoutCode : Code[20])
BodyFilePath := COPYSTR(FileManagement.ServerTempFileName('html'),1,250);
ReportLayoutSelection.SetTempLayoutSelected(LayoutCode);
REPORT.SAVEASHTML(ReportID, BodyFilePath, DocumentVariant);
ReportLayoutSelection.SetTempLayoutSelected('');

However, I'm getting an error "An invalid field or attribute has been specified for the 'Format' property", and it occurs when REPORT.SAVEASHTML is called. Am I doing this right, or is there another way to send Word Layouts as bodies in email?

Answers

  • JuhlJuhl Member Posts: 724
    You can find what you need in standard code, look at table 77 and codeunit 60.
    Also I wrote this extending on standard function, maybe you can be inspired https://juhl.blog/2017/11/05/making-body-in-document-mailing-multilingual/
    Follow me on my blog juhl.blog
  • rjsfrjsf Member Posts: 17
    Hi Juhl, thanks. Actually I already explored that route, but unfortunately the report does not fall on any of the preexisting Report Usage Types in table 77. It will actually be more difficult to use the routines in that table just to send a custom Word Layout email. All we need is to send a custom Word Layout in the background and we do not need the fancy UI from the standard routines.

    So that is why I already built this function and I just need help to find out why I'm getting the error message on SAVEASHTML.
  • JuhlJuhl Member Posts: 724
    Look at GetEmailBody in TAB77, i just used it last week, no problem.

    If you need it in Outlook, you need to move the file to the client.
    ReportSelections.GetEmailBody(ServerEmailBodyFile,ReportSelections.Usage,SalesInvoiceHeader,SalesInvoiceHeader."Bill-to Customer No.",EmailAdress);
    Body := GetBodyText(ServerEmailBodyFile);
    
    LOCAL GetBodyText(ServerFilePath : Text) Value : Text
    IF (ServerFilePath <> '') AND FileManagement.ServerFileExists(ServerFilePath) THEN BEGIN
      TempBlob.INIT;
      FileManagement.BLOBImportFromServerFile(TempBlob,ServerFilePath);
      TempBlob.Blob.CREATEINSTREAM(BlobInStream);
      BlobInStream.READ(Value);
      ERASE(ServerFilePath);
    END;
    
    Follow me on my blog juhl.blog
  • kabrocokabroco Member Posts: 111
    Did you set the "DocumentVariant : Variant" correctly? I can remember that I got a similar error-message, caused by a wrong DocumentVariant.
Sign In or Register to comment.