0
votes

Using OpenXML what I'm trying to do is insert a table into a Word document. This works fine, but now I need the table to break across two Word columns (as in the newspaper type columns in Word, not as in the column of the table itself). So in Word itself I took my table, added a section break before and a section break after and then set it to two columns and it breaks my table into two and distributes it across the columns. Looking at the layout in the Open XML productivity tool I see it added this before the table:

<w:p w:rsidR="00E634EE" w:rsidRDefault="00E634EE" w14:paraId="0D41C6E2" w14:textId="77777777" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
  <w:pPr>
    <w:sectPr w:rsidR="00E634EE" w:rsidSect="00C511F2">
      <w:pgSz w:w="12240" w:h="15840" />
      <w:pgMar w:top="864" w:right="864" w:bottom="1440" w:left="864" w:header="720" w:footer="720" w:gutter="0" />
      <w:cols w:space="720" />
      <w:docGrid w:linePitch="360" />
    </w:sectPr>
  </w:pPr>
</w:p>

And after my table it added this:

<w:p w:rsidR="00E634EE" w:rsidP="00E634EE" w:rsidRDefault="00E634EE" w14:paraId="15329047" w14:textId="77777777" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
  <w:pPr>
    <w:rPr>
      <w:noProof />
    </w:rPr>
    <w:sectPr w:rsidR="00E634EE" w:rsidSect="00E634EE">
      <w:type w:val="continuous" />
      <w:pgSz w:w="12240" w:h="15840" />
      <w:pgMar w:top="864" w:right="864" w:bottom="1440" w:left="864" w:header="720" w:footer="720" w:gutter="0" />
      <w:cols w:space="720" w:num="2" />
      <w:docGrid w:linePitch="360" />
    </w:sectPr>
  </w:pPr>
</w:p>

(interestingly the 2 columns are defined in the section break after the table and not before it).

So I edited my code to add section breaks before and after my table and it does produce columns...but my table doesn't break across the columns. Instead it's all stuffed in the left-most column.

So how, and where (if possible) can I indicate how and where to start putting table rows in the second column rather than the first?

These are the breaks that end up in my file:

<w:p w:rsidR="00EA3E62" w:rsidRDefault="003B0473" w14:paraId="6A8C371A" w14:textId="77777777" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
  <w:pPr>
    <w:sectPr w:rsidR="00EA3E62">
      <w:pgSz w:w="12240" w:h="15840" />
      <w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="720" w:footer="720" w:gutter="0" />
      <w:cols w:space="720" />
      <w:docGrid w:linePitch="360" />
    </w:sectPr>
  </w:pPr>
</w:p>
<w:tbl xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
    ...
</w:tbl>
<w:p w:rsidR="00EA3E62" w:rsidRDefault="003B0473" w14:paraId="784DE666" w14:textId="77777777" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
  <w:pPr>
    <w:sectPr w:rsidR="00EA3E62">
      <w:type w:val="continuous" />
      <w:pgSz w:w="12240" w:h="15840" />
      <w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="720" w:footer="720" w:gutter="0" />
      <w:cols w:space="720" w:num="2" />
    </w:sectPr>
  </w:pPr>
</w:p>

Added with (and running through Word Automation Services):

// Insert section breaks
var end = new Paragraph(
    new ParagraphProperties(
        new SectionProperties(
            new SectionType() { Val = SectionMarkValues.Continuous },
            new Columns() { Space = "720", ColumnCount = 2, Separator = true })
        )
    );
parent.InsertAfterSelf(end);
parent.InsertAfterSelf(t);
parent.InsertAfterSelf(new Paragraph(
    new ParagraphProperties(
        new SectionProperties(
            new SectionType() { Val = SectionMarkValues.Continuous })
        )
    )
);

Where parent is the element I'm adding my table after and t is my Table element.

1
Please add the minimal code to create repro. Also, which version of Word is involved? - Cindy Meister
I can't help with the XML but there is a table row property setting for allowing rows to break across pages or not. You might experiment with that and check how that translates into the XML code. - Rich Michaels
@RichMichaels which property are you referring to? I'm not seeing it. There's a child element for TableRowProperties called CantSplit but that doesn't look like the thing. - Matt Burland
@CindyMeister - I added some code, but the whole thing would be very large and I don't think entirely necessary. The question is what defines where a table will be split across two columns?. It's not a bug, or code that doesn't work (well, it kinda is). The question is how and where do I specify put these rows in column 1 and put these rows in column 2 - Matt Burland
@MattBurland I don't know OpenXML but I know Word VBA and there is a boolean of Selection.rows(1).AllowBreakAcrossPages = True and in your scenario of a 2-column page with a multi-row table, setting that boolean to True allows table content to flow to the 2nd column. Otherwise the whole table shifts to the 2nd column. - Rich Michaels

1 Answers

0
votes

After some experimentation, I believe I have now figured this out. The trick is, to add another section break.

The issue, I believe, is that without another section break following the table, the next break is at the end of the page, so Word will lay everything out in the first column until it reaches the end of the page. If there is enough space on the page, then the entire table will end up in one column (and look really lopsided). So the trick is to add another (empty) paragraph right after to force Word to compress the table by breaking it across columns.