Create word document using document template openXML dll RRS feed

  • Question

  • Hello,

    I have to create MS word document using one template file in C# using openxml dll.

    I do not have any idea how should I start.

    requirements is, In template there will be one page which will contain few information as below, and I need to repeat this block of content n'th times.


    Employee Name: #Name# 
    Address: #Address#
    Employee ID : #EmployeeID#
    Status: #Status#


    Now I need to repeat this section dynamically in c# code as per Employee List.

    SO if there are 5 employees this content should get repeat 5 times.

    How it is possible with Open XML.

    I could replace text of existing block to actual one.
    but unable to repeat that block / Page dynamically.

    Also I'm not sure how I should create template where I will get starting and ending point of content.
    I guess Document template needs to be create in proper format. Which I don't know :(

    Thanks in advance.

    • Moved by CoolDadTx Tuesday, June 28, 2016 2:05 PM Office related
    Tuesday, June 28, 2016 4:33 AM

All replies

  • I will give you the usual 3 part office disclaimer:

    1. If you can limit it to the new Formats (docx, xlsx) you can use OpenXML SDK. Or ZipArchive and XMLReader classes. these formats are public and little more then renamed .zip containers full off .xml files.

    2. If you need to support the old formats or old and new, you ahve to use the (t)rusty office COM interop. Wich has seveal pitfalls. Avoid if possible.

    3. For any given problem, source/target format and display technology there might be a 3rd Solution. But those are rare and often very specific.

    Here some articles on this themathic:

    Tuesday, June 28, 2016 11:09 AM
  • If the template you are trying to match is really as simple as what you showed in the initial post, I would just create the document from scratch using OpenXml. Look at the OpenXml documentation, tutorials and samples. You would initialize the document and then loop through your employees creating the specified output for each one.


    Tuesday, June 28, 2016 9:29 PM
  • Thanks for your reply.
    I know i will have to loop through employees and add t in the document.
    My problem is.

    I can not add predefined text / style from code.
    I will have to get specific block from template itself, and i need to repeat that bock "n"  times.

    That means, user will manage templates. using template I just need to fetch respective block, and change text of keyword to actual word. and repeat this for all emplyees.

    And i'm unable to fetch block from word, which i can repeat.

    • Proposed as answer by vangog63 Sunday, July 30, 2017 4:40 PM
    • Unproposed as answer by vangog63 Sunday, July 30, 2017 4:40 PM
    Wednesday, June 29, 2016 3:49 AM
  • Hi Gaurav,

    Do you mind try Word automation method? It is complex to achieve this with OpenXML SDK, you need to create content controls to store text, and then repeat these content controls.

    The link below might be useful to you.

    # Creating MS Word Content Controls on a loop

    Best Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, June 29, 2016 6:06 AM