In many cases there is a requirement to generate Microsoft word doc , which contains standard template data as well as dynamic variable data for creating an attachment.
Though there is standard functionality to create attachment in CRM with templates , but many times it may not suffice our requirement .
In this blog , I will provide steps which can be used to generate Microsoft word documents as attachment with dynamic data.
Software Required : Microsoft Word Content Control Tool Kit
Step 1 : Create a Microsoft word template using the Content controls.
You can create the Microsoft word template , which contains the fixed text as well as dynamic data that you want to populate. The dynamic data will be handled by content controls at run time .
Content controls are predefined pieces of contents that include text blocks, drop-down menus, check boxes, and pictures, etc. You can map content controls with elements in an custom XML file so that the data can be automatically populated in the document.
Step 2: Binding content controls to the custom XML part
Open the Word template you have just created in step 1 in word content control toolkit . Bind the content controls to Custom XML parts.
Step 3 : Uploading the template to the MIME folder
Now upload the word template with content controls into the Mime repository in the SAP system .
Go to SE80 and select
MIME Repository, and expand to folder . You can select the folder in which you want to upload the template.
Right-click your folder and choose
Import MIME Objects. Select and upload the template.
Create New Document screen, select
Application-Specific MIME Object as the document class, enter a document name, and choose
Step 4 : Generate the XML data
Go to transaction XSLT_TOOLS and create a simple transformation Z_XML_GEN.
This transformation will contain the dynamic data that you want to fill into the template . In our case we want to fill the Sold to name and sold to number from the SAP system .
Hence we write this transformation.This will generate custom XML with the data for sold to name and sold to id.
Step 5: Fetch the Word Document template
Now in your program (where you actually want to generate the word template ), first we need to fetch the word template which we uploaded in Mime repository (step 3). It will be loaded in variable docx.
Step 6: Fetch Data and Call the transformation to generate custom XML
Now fill the data Sold to name and sold to id into structure ls_data (this structure can contain 2 fields sold_to_name and sold_to_id). And call the transformation Z_GEN_XML with this data . The transformation will generate the custom XML with the data for sold to name and sold to id. This will be stored in variable ‘texts’
Step 7: Create the style sheets for the word template .
In step 5 we had fetched the word template from the mime repository into variable docx. Now we generate style sheet for this docx. Data stored in variable lt_xslttab.
Step 8 : Merge data
Merge the word template style sheet created in step 7 with the XML data generated from transformation in step 6.
Step 9 : Generate the attachment object with the merged data
lv_doc now contains the template data along with the merged data . Make sure everything is in XSTRING format. With this you can now create a attachment object(the programming for this is easy , you can simply refer the standard coding for that ) and display it in the web UI.