web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested answer

"Email Message".Create - How to read body from a Word Template

(2) ShareShare
ReportReport
Posted on by 833

Regarding the new Enhanced Emailing in BC: I'm using the Create method of the "Email Message" Codeunit and the OpenInEditorModally method of the Email Codeunit. At the moment I'm hard-coding the message-body in my AL Code but I would like to pull the body in from a Word Template and display it in the Message portion of the "Email Editor" page (ID: 13). Is this possible? Ideally, I'd like to be able to embed placeholders in the template that would be populated from BC data

(e.g.) . This email relates to Reference No: <RefNo>

I have the same question (0)
  • Suggested answer
    Nitin Verma Profile Picture
    21,684 Moderator on at
    RE: "Email Message".Create - How to read body from a Word Template

    Hi Nick Webb ,

    Please visit the below link it will explain you how to add Word layout for email body.

    https://business-central.to-increase.com/md/en-US/ui-how-send-documents-email

    https://yzhums.com/19955/

  • Suggested answer
    MahGah Profile Picture
    15,529 on at
    RE: "Email Message".Create - How to read body from a Word Template

    Just to add to this also see below. But I do not know how new Enhanced Emailing works with these suggestions.

    www.youtube.com/watch

  • Nick Webb Profile Picture
    833 on at
    RE: "Email Message".Create - How to read body from a Word Template

    Thanks all for your help. I'm now reading my Word template doc and displaying the content in the Email Editor page. However, I wanted to simulate the user editing the template document in BC. So, I went into Custom Report Layouts and performed the following steps:-

    1. New -> New -> Edit - Insert Built-in Layout for a Report.
    2. Specified my template ID, switched on 'Insert Word Layout' and clicked [OK].
    3. Selected my Report ID -> Layout -> Export Layout
    4. Opened the exported document in Word, changed some text and saved it.
    5. Back in BC, -> Layout -> Import Layout -> Chose my edited document and clicked [Open]
    6. Layout -> Update Layout -> System reports: The Word layout has been updated to use the current report design.
    7. Process -> Run Report -> changes are displayed.

    However when I run my extension the original wording is displayed. Why? Here's my procedure:-

    local procedure GetBodyTextFromTemplate(var recLogSheet: Record LogSheets; var txtBody: Text): Boolean
    var
       OutS: OutStream;
       InS: InStream;
       TempBlob: Codeunit "Temp Blob";
       RecRef: RecordRef;
       bReturn: Boolean;
    begin
       txtBody := '';
       TempBlob.CreateOutStream(OutS);
       RecRef.Get(recLogSheet.RecordId);
       bReturn := Report.SaveAs(50051, '', ReportFormat::Html, OutS, RecRef);
       if bReturn then begin
          TempBlob.CreateInStream(InS);
          InS.ReadText(txtBody);
       end;

       Exit(bReturn);
    end;

  • Suggested answer
    Nitin Verma Profile Picture
    21,684 Moderator on at
    RE: "Email Message".Create - How to read body from a Word Template

    Hi,

    Please change as per below

    base64: Text;

    txtBody := '';

      TempBlob.CreateOutStream(OutS);

      RecRef.Get(recLogSheet.RecordId);

      bReturn := Report.SaveAs(50051, '', ReportFormat::Html, OutS, RecRef);

      if bReturn then begin

         TempBlob.CreateInStream(InS);

         InS.ReadText(txtBody);

      end;

      base64 := Base64Convert.ToBase64(InS);

    EmailMsg.AddAttachment('Order ' + '.docx', '.docx', base64);

    Exit(bReturn);

  • Nick Webb Profile Picture
    833 on at
    RE: "Email Message".Create - How to read body from a Word Template

    Thanks for your response. Perhaps I should've mentioned that the text returned from my procedure goes on to become the body of an email message (as below):-

    GetBodyTextFromTemplate(recLogSheet, txtEmailBodyText)

    EmailMessage.Create(lstRecips, EmailSubject, txtEmailBodyText, true, lstCC, lstBC);

    enumEmailAction := Email.OpenInEditorModally(EmailMessage, "Email Scenario"::"Sales Invoice");

    My question remains: When the report is updated in BC by a user why doesn't my code use the latest version of it?

  • Rudylar Profile Picture
    103 on at
    "Email Message".Create - How to read body from a Word Template
    Hi,
    Hopefully this will reach somebody despite of being "answered".
    The suggested answer is using the "Report.SaveAs()" method, which is a method for "Reports", not for "Word Templates". The post subject is mentioning "Word Template".
    The Word Templates do not have Report number... 
    I have not been able to extract the content from a Word Template... 
    var
                            WordTemplateRecord: Record "Word Template";
                            WordTemplateCU: Codeunit "Word Template";
                            WordTemplateToTextWizard: Page "Word Template To Text Wizard";
                            word1: Codeunit "Word Template Field Value";
                            WordTemplateCU2: Codeunit "Word Template Interactions";
                            InS2: InStream;
                            InS3: InStream;
                            TempBlob2: Codeunit "Temp Blob";
                            OutS2: OutStream;
                            test1, test2 : Text[250];
    begin
                            WordTemplateRecord.Reset();
                            WordTemplateRecord.SetRange(Code, 'COMMANDE_ENG_ACKN ORDER');
                            TempBlob2.CreateOutStream(OutS2);
                            TempBlob2.CreateInStream(InS2);
                            TempBlob2.CreateInStream(InS3);
                            if WordTemplateRecord.FindFirst() then begin
                                WordTemplateCU.Load(WordTemplateRecord.Code);
                                WordTemplateCU.GetTemplate(InS2);
                                WordTemplateCU.GetDocument(InS3);
                            end;
                            InS2.ReadText(test1);
                            InS2.Read(test1);
                            InS3.Read(test2);
    end
    If anybody can help...
    Thanks 
  • Suggested answer
    YUN ZHU Profile Picture
    92,797 Super User 2025 Season 2 on at
    "Email Message".Create - How to read body from a Word Template
    I think you can try to convert Word to html first, and then read the content.
    More details: Dynamics 365 Business Central: Convert Word (.docx) To HTML (.html) within BC (No control add-in and APIs)
     
    Thanks.
    ZHU
  • Suggested answer
    Khushbu Rajvi. Profile Picture
    19,160 Super User 2025 Season 2 on at
    "Email Message".Create - How to read body from a Word Template
  • Suggested answer
    Sohail Ahmed Profile Picture
    11,089 Super User 2025 Season 2 on at
    "Email Message".Create - How to read body from a Word Template
    Yes, this is possible with some AL customization.
     
    To load a Word Template and use it as the email body, you’ll need to:
     
    Steps:
     
    1. Store your Word template in a report layout or as a resource (e.g., on a BLOB field or in media).
     
     
    2. Use Report.SaveAs or ReportLayoutSelection to generate the body as HTML.
     
     
    3. Pass the HTML content into EmailMessage.Create() as the body parameter.
     
     
    4. For placeholders like <RefNo>, replace them with actual values using Text.Replace() before assigning to the email body.
     
     
     
    There’s no built-in token replacement like Mail Merge, so manual text substitution is needed.
     
    Let me know if you want a sample AL snippet.
     
    Mark below checkbox to make this answer Verified if it helps you.
     
     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
Rishabh Kanaskar Profile Picture

Rishabh Kanaskar 4,285

#2
Sumit Singh Profile Picture

Sumit Singh 2,725

#3
Nimsara Jayathilaka. Profile Picture

Nimsara Jayathilaka. 2,526

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans