none
Populating a Table with XML in Word RRS feed

  • Question

  • I have a document with content controls in it that I am populating by binding it to fields in XML using using the Content Control Toolkit.

    Now I want to populate a Table from repeating entries in the XML. It seems that there should be a way to bind a table to an XPath statement that would produce multiple rows but I don't see how to do it.

    Does anyone know whether the issue is in the Toolkit or in Word itself?

    So for example would Word understand the XPath Syntax if I could find a way to bind the columns?

    Is there some way to embed an Excel table and then bind it to rows in the XML?

    I am using the Open XML SDK to stuff the XML into the document so I understand that I could push the table in as well but this seems like such a common requirement that I want to avoid coding it if someone has come up with a creative solution.

    Thanks,
    Don Rule
    http://translationalsoftware.com

    Wednesday, December 21, 2011 5:34 PM

Answers

  • The right answer to this question is "Don't try this at home" unless you already have some experience with the SDK and xml format.

    What I ended up doing was to put my tables within Content Controls because that seemed to be the easiest way to figure out programatically what I wanted to put where. I put an XPath statement in the Tag of the content control to tell me what collection I wanted to populate the table with.

    For a grid I make a copy of the table and look at the text content in each cell and replace it with the value of that attribute.

    When I put the word "Portrait" in the content control it means that the table itself repeats for each instance of the collection. In this case I make a copy of the table and replace each bracketed item with the attribute from the instance of the collection (e.g. [TitleAttribute]). 

    The big problem is that the SDK exposes the XML structure but does nothing to abstract it. So you have to know the nuances of how to navigate the document structure. I still can't figure out how to consistently create the same type of content control every time (SdtBlock vs. SdtRun vs. SdtCell) so I just save one that works and copy it.

    There is a company called Windward that has a tool for creating Word reports that seems pretty nice. It is outside my startup budget right now but that is definitely the way to go.

    • Marked as answer by Don-Rule Wednesday, January 18, 2012 11:50 PM
    Wednesday, January 18, 2012 11:50 PM

All replies

  • Hi Don

    <<Now I want to populate a Table from repeating entries in the XML. It seems that there should be a way to bind a table to an XPath statement that would produce multiple rows but I don't see how to do it.

    Does anyone know whether the issue is in the Toolkit or in Word itself?>>

    Unfortunately, Word doesn't support anything like this...

    You're right, it's requested fairly often. And for a while something along these lines worked with the XML Nodes functionality that was introduced in Word 2003, but had to be pulled out a few years ago due to a court decision. So you have to do it all with your code.


    Cindy Meister, VSTO/Word MVP
    Thursday, December 22, 2011 2:11 PM
    Moderator
  • Hi Don,
     
    Have you figured out the problem yet? If you still show any concern on the problem, just feel free to let us know.
     
    Best Regards,
    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Monday, December 26, 2011 9:48 AM
  • This is still a huge problem. Here is a post that I am working from to try to solve it but it is not straightforward by any means http://msdn.microsoft.com/library/cc197932(office.12).aspx

     

    • Marked as answer by Don-Rule Wednesday, January 18, 2012 11:19 PM
    • Unmarked as answer by Don-Rule Wednesday, January 18, 2012 11:37 PM
    Friday, December 30, 2011 1:24 AM
  • Hi Don-Rule,

    Does the article help you solve the problem? Do you still need any assistant about the problem? Feel free to follow up after you have tried the solution.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Tuesday, January 3, 2012 2:55 AM
  • The right answer to this question is "Don't try this at home" unless you already have some experience with the SDK and xml format.

    What I ended up doing was to put my tables within Content Controls because that seemed to be the easiest way to figure out programatically what I wanted to put where. I put an XPath statement in the Tag of the content control to tell me what collection I wanted to populate the table with.

    For a grid I make a copy of the table and look at the text content in each cell and replace it with the value of that attribute.

    When I put the word "Portrait" in the content control it means that the table itself repeats for each instance of the collection. In this case I make a copy of the table and replace each bracketed item with the attribute from the instance of the collection (e.g. [TitleAttribute]). 

    The big problem is that the SDK exposes the XML structure but does nothing to abstract it. So you have to know the nuances of how to navigate the document structure. I still can't figure out how to consistently create the same type of content control every time (SdtBlock vs. SdtRun vs. SdtCell) so I just save one that works and copy it.

    There is a company called Windward that has a tool for creating Word reports that seems pretty nice. It is outside my startup budget right now but that is definitely the way to go.

    • Marked as answer by Don-Rule Wednesday, January 18, 2012 11:50 PM
    Wednesday, January 18, 2012 11:50 PM