One of the sessions I attended at Directions North America in May 2023 was Jesper Schulz’ session, “Enhanced Interactions using mail merge with Word Templates”. Jesper works with Microsoft as Engineering Manager on the Microsoft Dynamics 365 Business Central team. I picked the session because I was curious on Microsoft’s intentions of using Mail Merge, since at a previous job I worked with an ISV solution that generated word documents from Dynamics NAV using primary and related entities. Moreover, I am curious on how Microsoft is continuously integrating its own products with Business Central.
Let me share with you what I learnt.
The official Microsoft document on Word Templates can be found here.
You can continue to read my demonstration below or you can watch my video or both, as some things are mentioned in blog while others are only in the video.
Subscribe to my channel if you want to get notified as soon as my Business Central tips for business users, consultants and developers become public.
Word Templates at a Glance
First, Mail Merge and Word Templates are built around a primary entity. In my example I picked the Customer entity.
Essentially, with Word Templates, the system is generating an Excel file DataSource.xlsx with all the fields that you’re interested to use in your Word Template. You have the option of selecting which fields you want so you don’t include data that is not essential in your interaction. For AL aficionados, these fields are the equivalent of a column (control; field) from under a DataItem in an AL report.
Then, with the Excel file acting as DataSource, you can edit the Word Template. The wizard allows you to start from a blank template and you can start writing your document and seed it with fields from the DataSource. When you’re happy with your template you can upload it to your Word Template record.
The Word Template can be applied to a filtered set of its primary data, or can be triggered from the primary data itself.
End result is a set of word documents, if you requested the document to be split in many word documents or, one document with all entities you filtered by.
Consultants and users alike can add additional correlated entities or totally un-related entities. We will investigate further what this means and how it’s done.
Start from the Word Template wizard
In this exercise, a Cronus employee will create an invitation announcing a 25% off on all furniture for all customers located in the city of Tampa, Florida, to celebrate Tampa’s 200 years anniversary.
In Business Central version 22 (global) search for Word Templates. You will find 3 samples there, for customer, vendor and contact entities.
You can edit, inspect them or remove them.
But if you want to create your own click on New.
First screen of the wizard is Create a Word Template window:
You can choose one of the existing entities (Customer or Vendor in my screen) or click on the link Add new entity if you want to build a template based on a different entity.
Let’s choose Customer and click Next:
Choose a related entity
As we want to address the event discount letter to the primary contact for each customer, we need to add table Contact, as a related entity. Click on the action bar Add related entity:
Pick the related entity table number,
Choose the field in the customer table that connects to the related table, in my case, Primary Contact No. Lastly, choose a prefix for the new table.
Note: All fields chosen in the next step from Contact table will be prefixed with “CONTA_”.
Notice in the next screen how the system picked a few fields from both tables, primary and related tables.
Drill down on 24 and on 27 to refine the fields that will be included in the data set by using Mark as included or Mark as Excluded actions:
There is a limit of 250 fields for all entities, related or un-related.
On the next screen, click on Download a blank Template:
Once downloaded, open the template and start editing the letter, using fields from the data set.
Open the downloaded zip, and you can find 2 files, an Excel file, containing the fields in the dataset, and a Word document containing the block template:
Open both and inspect the content:
When opening the Word document choose Yes when you’re asked to include the data from the dataset in your Word document:
Open the Mailing menu and choose Insert Merge Field action from the ribbon:
Notice how Customer fields and Contact fields are listed, but Contact table fields are prefixed with “CONTA_”.
Write your invitation, or ask ChatGPT to do it:
Choose an un-related entity
We might want to add Cronus company address in the header of the Word template. For that, we need to add an un-related entity: table Company Information and include the address fields.
Go to the next screen in the wizard and upload your newly created template:
Save your Word Template:
Let’s test the Word Template
To run your Word Template and generate the letters, select your Word Template and click Apply:
You can generate the output as Word documents, PDF, html, or simply text.
You can also split the documents, instead of one large document with a page for each customer, have “n” word documents, one per customer …
Set the filters accordingly. In my case I want to apply the template to all customers that are located in Tampa.
And the final screen of the wizard, an overview page:
Clicking on Finish triggers the merging of the dataset with the Word Template and the creation of the Output.docx. As there were only 2 customers for which I switched the city to Tampa, we’ve got 2 pages, one for each customer. The second page does not have the contact name after “Hello”, as I did not assign a Primary Contact No. for the second customer; “Primary Contact No.” acts as the connector field to Contact table, therefore no Contact data:
Leverage Word Templates when sending emails from BC
Some entities, like Customer or Vendor allow users to send emails.
On the Customer Card, navigate to Send Email (Actions -> Other-> Send Email).
On the page Compose an Email you can use the output of a Word Template as the email body. Use Word Template to achieve that.
Or, you can attach the output of a Word Template to the email. In fact you can use more than one Word Template, if you have more than one template for the entity (Customer in my case). You just have to attach them sequentially.