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

/wp-content/uploads/2016/02/pic1_889367.png

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.


/wp-content/uploads/2016/02/pic2_889373.png

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.

On the Create New Document screen, select Application-Specific MIME Object as the document class, enter a document name, and choose Save.



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.


/wp-content/uploads/2016/02/pic3_889374.png


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.



/wp-content/uploads/2016/02/pic4_889375.png


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’


/wp-content/uploads/2016/02/pic5_889383.png


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.


/wp-content/uploads/2016/02/pic6_889384.png


Step 8 : Merge data


Merge the word template style sheet created in step 7 with the XML data generated from transformation in step 6.



/wp-content/uploads/2016/02/pic8_889398.png


/wp-content/uploads/2016/02/pic7_889397.png


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.


/wp-content/uploads/2016/02/pic9_889399.png


To report this post you need to login first.

1 Comment

You must be Logged on to comment or reply to a post.

  1. Nicolas Raymond

    Hi Poonam,

    Thanks for the tutorail.

    i have tried to implement it in our system, but I had error during the generation_docx with the xml schema which seems to be wrong. Is it possible to get the Word and the xml file you used to execute your process?

    Thanks in advance

    regards

    Nicolas Raymond

    (0) 

Leave a Reply