none
How to bind runtime object datasource to Word Document template in C# Winforms? RRS feed

  • Question

  • I am writing a C# Winforms application that needs to generate Word Document reports from an existing template with content controls. I have went through https://msdn.microsoft.com/en-us/library/ms178805.aspx which describes how to add databinding within the word VSTO environment.

    However I cannot find any information on how to bind to Word Document from custom application. Could anyone give some advice? Thanks.


    Jake See


    Saturday, August 15, 2015 10:25 PM

Answers

  • Hi Jake

    Mmmm. Could you explain why you'd be trying to link a schema to a Word document? The features with which that was associated were mostly removed a few years ago due to a court decision. It's mainly there, still, for VSTO document-level solutions (the entry in your screen shot).

    If you really want to use the API, you can address content controls by ID, Tag or Title, rather than relying on their position in the document. ID is, as I recall, a valid index - ContentControls[index] - but since that's GUID assigned by Word it's not really used so much. The other two are methods on the document object and return a collection: GetContentControlsByTitle and GetContentControlsByTag.

    <<I have some understanding of OpenXML as a kind of Word Document serializer without using the Word Application interop APIs. But this seems too low-level having to mess with the Word Document markup itself. Is it?>>

    That depends on how you go about it. It's definitely a learning curve, but with the Open XML SDK it's a lot more like working with the object model than if you'd have to work directly with the Packaging and XML. Especially, if you were to access a single Custom XML Part - an XML file in the vocabulary of your specification - linked to content controls and substitute the "mail merge" data for the "placeholder" data you wouldn't need to learn or code a lot. This assumes that a document "template" (docx) with already prepared and linked content controls is provided to the user. If people are moving things around, however, things might be a bit more complicated.

    One advantage of which you should be aware is that the Open XML approach would continue to work in a server-side setting, as well (assuming write permissions to the document). You also wouldn't run into "the file is already open" conflicts if more than one user wants to create a document.


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, August 18, 2015 3:28 PM
    Moderator

All replies

  • Hi Jake

    Could you please be more specific about what you have in mind when you use the term "bind"?

    Do you mean just drop data into a Word document? Or do you have something more permanent in mind?

    Word has the concept of "mail merge", which is the way end-users link to a data source in order to produce documents that are essentially the same, except for the data coming from the data source (database, memory streams or similar are not supported). This is the closest Word has to "data binding".

    I suspect that for your purposes the most efficient and scalable approach would be to leverage the Office Open XML file format to write data to the closed Word file, either directly to the content controls or to a Custom XML Part's nodes linked ("bound") to the content controls.

    Here are links to some articles that discuss linking content controls and Custom XML Parts as well as using the Open XML SDK. Note that I don't find anything in a quick search that uses the most recent version of the Open XML SDK (2.5) that makes life a LOT easier. These links are mostly to give you an idea of how the "binding" between hte two works so that you can decide whether the approach is appropriate:

    https://msdn.microsoft.com/en-us/library/office/gg605188(v=office.14).aspx
    https://msdn.microsoft.com/en-us/library/bb510135(v=office.12).aspx
    http://blogs.msdn.com/b/ericwhite/archive/2008/10/19/creating-data-bound-content-controls-using-the-open-xml-sdk-and-linq-to-xml.aspx
    http://openxmldeveloper.org/blog/b/openxmldeveloper/archive/2010/10/27/59361.aspx


    Cindy Meister, VSTO/Word MVP, my blog

    Sunday, August 16, 2015 2:24 PM
    Moderator
  • Hi Cindy,

    (I have not read the links you give, but just want to elaborate on my problem in case you have better advice. Thanks).

    I have some understanding of OpenXML as a kind of Word Document serializer without using the Word Application interop APIs. But this seems too low-level having to mess with the Word Document markup itself. Is it?

    I am thinking Custom XML is the way so that I can do "databinding" from runtime object source to the XML elements. However, I still don't understand Custom XML and XSD schema despite reading many articles on the internet. If I can use Word Application and attach an XML to the Word Document (which I don't know how to, keep running into error saying cannot install XML expansion or schema, see below), how can I open the Word Document in my Winforms application and retrieve the attached Custom XML and change its contents so that the Word Document gets updated?

    Currently, I use the Word Application interop API to iterate the content controls in sequence and populate the fields, but this breaks when the content control position gets changed. I want to use a method without Interop API, have the content controls be binded to XML, with repeatable sections, then use runtime code to update the XML by binding runtime object instance to XML and push updates to the content controls.

    Would you be able to provide more specific advice on how to achieve this? Can you provide a basic sample Custom XML that I can install on my Word Application? I have tried several online tutorials to no avail.

    Thanks.


    Jake See


    • Edited by JakeSee Monday, August 17, 2015 10:54 AM
    Monday, August 17, 2015 10:53 AM
  • Hi Jake,

    >> If I can use Word Application and attach an XML to the Word Document (which I don't know how to, keep running into error saying cannot install XML expansion or schema, see below), how can I open the Word Document in my Winforms application and retrieve the attached Custom XML and change its contents so that the Word Document gets updated?

    In my option, if you want to change the xml value which you attached, I am not sure why you use the Templates and Add-ins. For retrieving the attached custom XML and change its contents, you could use Open XML SDK, and please check the link which is provided by Cindy.

    #Creating Data-Bound Content Controls using the Open XML SDK and LINQ to XML
    http://blogs.msdn.com/b/ericwhite/archive/2008/10/19/creating-data-bound-content-controls-using-the-open-xml-sdk-and-linq-to-xml.aspx

    >> but this breaks when the content control position gets changed

    I am not sure what you mean by this breaks when the content control position gets changed, did you get any error or unexpected result. As far as I know, you could get the content control object by index, and you could set the text of the content control.

    # ContentControl Object (Word)
    https://msdn.microsoft.com/en-us/library/office/ff821215.aspx

    >> I want to use a method without Interop API, have the content controls be binded to XML, with repeatable sections, then use runtime code to update the XML by binding runtime object instance to XML and push updates to the content controls

    For this, please refer the link below, it would be helpful if you could share us what you test, and what result you get.

    Best Regards,

    Edward


    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.


    Tuesday, August 18, 2015 8:18 AM
  • Hi Jake

    Mmmm. Could you explain why you'd be trying to link a schema to a Word document? The features with which that was associated were mostly removed a few years ago due to a court decision. It's mainly there, still, for VSTO document-level solutions (the entry in your screen shot).

    If you really want to use the API, you can address content controls by ID, Tag or Title, rather than relying on their position in the document. ID is, as I recall, a valid index - ContentControls[index] - but since that's GUID assigned by Word it's not really used so much. The other two are methods on the document object and return a collection: GetContentControlsByTitle and GetContentControlsByTag.

    <<I have some understanding of OpenXML as a kind of Word Document serializer without using the Word Application interop APIs. But this seems too low-level having to mess with the Word Document markup itself. Is it?>>

    That depends on how you go about it. It's definitely a learning curve, but with the Open XML SDK it's a lot more like working with the object model than if you'd have to work directly with the Packaging and XML. Especially, if you were to access a single Custom XML Part - an XML file in the vocabulary of your specification - linked to content controls and substitute the "mail merge" data for the "placeholder" data you wouldn't need to learn or code a lot. This assumes that a document "template" (docx) with already prepared and linked content controls is provided to the user. If people are moving things around, however, things might be a bit more complicated.

    One advantage of which you should be aware is that the Open XML approach would continue to work in a server-side setting, as well (assuming write permissions to the document). You also wouldn't run into "the file is already open" conflicts if more than one user wants to create a document.


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, August 18, 2015 3:28 PM
    Moderator