none
Create word 2010 document (.docx) from a Mail Merge Template (.dotx) using OpenXML in VS2012 RRS feed

  • Question

  • Hi

    I need to create a Word 2010 document from Word 2010 Templates using OpenXML, VS 2012, C#.

    I have created a word 2010 template (.dotx) using a SQL view and kept that template in the Server, now I need to create document based on the database record(s) from the client application (Window application or web application - need both facility)

    Can anyone tell all the steps involved here also sample code or any links would be helpful?

    one more question, whenever I open the template , a warning message shown that “Opening this document will run the following sql”

    If I create a document from the template using OpenXml, Will that message be shown when user opens the generated document?  

    Regards

    MSK


    • Edited by mgsk Thursday, December 12, 2013 5:17 PM
    Thursday, December 12, 2013 5:15 PM

Answers

  • Hi mgsk

    I don't think we'll be able to answer this in one reply - we have to get the ground rules straight, first...

    1) You'd be better off if this were a document, not a template. It can be done with a template, but that would involve additional steps. If you can create a new document from this template, then save that as your Open XML SDK "template" it will save you some trouble.

    2) At the same time, you should remove the link to the data source in the document and turn the document back into a "plain" Word document. This can be done in the "Mailings" tab, in the first step, where you select whether you want to create letters, envelopes or labels. This will get rid of the message, but it should leave the merge fields, which is what you want to work with. Again, this could be done in your code, but life is simpler if you can remove anything you don't need to begin with.

    3) You next have to analyse what kinds of fields are in the document. For example, are there any nested fields, such as IF fields? Fields in the headers, footers or in any graphics? You have to know this in order to figure out whether your code needs to do the If (or other) calculations, on the one hand, and where you have to look for fields on the other.

    This is something of an FAQ, so you will find a couple of discussions on the topic here in the forum that may give you additional information you can use.


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, December 13, 2013 10:08 AM
    Moderator
  • Hi MSK,

    According to your description, you want to create a Word document from template by Open XML.

    I found a blog which described how to create a word document from a template using Open XML.

    Creating a new Microsoft Word document from a template using OpenXml

    From this blog, the ChangeDocumentType operation is critical to this process. This operation, provided by the OpenXml SDK, ensures the document is no longer marked as a Template but rather as a Document.

    >> If I create a document from the template using OpenXml, Will that message be shown when user opens the generated document?  <<

    Did you mean you create the Word document which contains the mail merge field?

    If so, when we open the document, the Word application will display the message to confirm with users whether you want to run the mail merge.

    You can get more information about mail merge:

    Resume a mail merge

    Best regards

    Fei


    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.

    Friday, December 13, 2013 9:59 AM
    Moderator

All replies

  • Hi MSK,

    According to your description, you want to create a Word document from template by Open XML.

    I found a blog which described how to create a word document from a template using Open XML.

    Creating a new Microsoft Word document from a template using OpenXml

    From this blog, the ChangeDocumentType operation is critical to this process. This operation, provided by the OpenXml SDK, ensures the document is no longer marked as a Template but rather as a Document.

    >> If I create a document from the template using OpenXml, Will that message be shown when user opens the generated document?  <<

    Did you mean you create the Word document which contains the mail merge field?

    If so, when we open the document, the Word application will display the message to confirm with users whether you want to run the mail merge.

    You can get more information about mail merge:

    Resume a mail merge

    Best regards

    Fei


    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.

    Friday, December 13, 2013 9:59 AM
    Moderator
  • Hi mgsk

    I don't think we'll be able to answer this in one reply - we have to get the ground rules straight, first...

    1) You'd be better off if this were a document, not a template. It can be done with a template, but that would involve additional steps. If you can create a new document from this template, then save that as your Open XML SDK "template" it will save you some trouble.

    2) At the same time, you should remove the link to the data source in the document and turn the document back into a "plain" Word document. This can be done in the "Mailings" tab, in the first step, where you select whether you want to create letters, envelopes or labels. This will get rid of the message, but it should leave the merge fields, which is what you want to work with. Again, this could be done in your code, but life is simpler if you can remove anything you don't need to begin with.

    3) You next have to analyse what kinds of fields are in the document. For example, are there any nested fields, such as IF fields? Fields in the headers, footers or in any graphics? You have to know this in order to figure out whether your code needs to do the If (or other) calculations, on the one hand, and where you have to look for fields on the other.

    This is something of an FAQ, so you will find a couple of discussions on the topic here in the forum that may give you additional information you can use.


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, December 13, 2013 10:08 AM
    Moderator
  • Thanks for your comments Cindy. Let me check
    Friday, December 13, 2013 3:55 PM
  • Thanks Fei ..let me check the blog and try to implement and get back to you

    Friday, December 13, 2013 3:56 PM
  • Hi mgsk,

    Since we haven't heard from you for a long time, I temporarily close this case. I mark useful reply as answer. If you have any concerns, please free feel to reopen it or submit a new question.

    Thanks for your understanding.

    Best regards

    Fei


    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.

    Thursday, December 19, 2013 12:05 PM
    Moderator