none
How to merge the tables in OpenXML the way the Word does it?! RRS feed

  • Question

  • Hello,

    I'm using OpenXML to create the Word document with multiple tables. However, I've noted that if I append two tables to each other then the second tables is appended to the first cell of the source table :(

    For example,  I have the Table A  
    |-----------|------------|-------------|
    | Cell AA  |   Cell AB  |  Cell AC   |
    |-----------|------------|-------------|
    (picture 1)

    and I want to add the following Table B, to my Table A

    |-----------|------------|
    | Cell BA  |   Cell BB  |
    |-----------|------------|
    (picture 2)

    I do it via OpenXML, using doc.MainDocumentPart.Document.Body.AppendChild and I end up with the following structure:

    |----------------------- |------------|-------------|
    | Cell AA                  |   Cell AB  |  Cell AC   |
    |------------------------|------------|-------------|
    | Cell BA  |   Cell BB  |
    |-----------|------------|
    (picture 3)

    where the table B is appended to the first cell and tables are not aligned by the width (opening the document in OpenXML Productivity Tool I see that I have two tables next to each other). 

    I cant understand why this is happening. I did a lot of research an experiments and found out that if I insert the empty paragraph between two calls of AppendChild(table) I have the normal structure that is aligned by the widht of the document, and the second table is not attached to the cell

    |-----------|------------|-------------|
    | Cell AA  |   Cell AB  |  Cell AC   |
    |-----------|------------|-------------|
    <emply line>
    |--------------------|-----------------|
    | Cell BA             |   Cell BB        |
    |------------------- |-----------------|
    (picture 4)


    Now, I'm trying to find the solution how to add tables to have the normal structure without empty paragraph, where tables have the same width and not attached to the specific cell.

    I tried to delete the paragraphs programmatically after the document is constructed, but it returns me back to picture 3, when tables are attached to the single cell :(  Moreover, the most interesting stuff comes from the Word - if I open that document (picture 4, with the empty paragraph) in Word and delete the empty paragraph manually I end up with the proper structure I'm trying to achieve 

    |-----------|------------|-------------|
    | Cell AA  |   Cell AB  |  Cell AC   |
    |-----------|------------|-------------|
    | Cell BA             |   Cell BB        |
    |------------------- |-----------------|

    but I can't find the way why deleting empty paragraphs via the OpenXML API won't do the same what Word does. It looks like that Words apply specific operations to tables.


    Can somebody shed the light on such strange behavior of OpenXML vs Word?!

    SharePoint 2007 - 2010 Tips & Tricks Portal | Microsoft MVP | My Blog about Information Management | My twitter
    Wednesday, March 10, 2010 1:05 AM

All replies

  • Hi Michael,

    Thanks for your question.

    You just raised an interesting problem of inserting a table. It seems that you haved inserted the second table into a cell of the first table according to your description. To help solve the problem, could you describe your scenario in detail? For example, would you mind show us the content of your xml file which contains the table? Or the code on how you insert the table using Open XML SDK?

    Thanks,

    Lu
    Wednesday, March 10, 2010 5:19 AM
  • Hi Michael,

    How is your problem going? Have you figured it out or still have trouble with that? If you have solved the problem, could you describe where the problem is and how you solve it. If not, what else coulde I help? Would you mind show us the content of your xml file which contains the table? Or the code on how you insert the table using Open XML SDK? Your feedback is very important to us.

    Thanks,

    Lu

    Monday, March 29, 2010 9:16 AM
  • Hello,

    I don't have the workaround yet. If you have the small and simple solution demonstrating this problem you then could post it here, so MS people are able investigate it. I had large project where extracting the bits to demonstrate the problem was the time consuming task for me. 


    SharePoint 2007 - 2010 Tips & Tricks Portal | Microsoft MVP | My Blog about Information Management | My twitter
    Wednesday, March 2, 2011 11:31 PM
  • Hi Michael,

    Can you please provide the following 2 documents:

    a. One you generated using Open XML
    b. One which has a fix. Basically in which you used Word UI and deleted the paragrapgh manully and it worked.

    We can compare them to see if Word has made any changes to other xml parts as well. 

    Thanks
    Ankush
    http://ankushbhatia.wordpress.com

     

     

     


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. Regards, Ankush Microsoft Online Community Support
    Thursday, March 3, 2011 7:48 PM