EstevaoLuis
EstevaoLuis

Reputation: 2542

ContentControl.insertOoxml adds extra paragraphs

I am trying to add to my Word document a new content control with a table inside. I have the Ooxml of the table and I am inserting this to a new content control with insertOoxml. But it is adding a new paragraph before the table and another one after it, as shown in the following image.

Table with extra paragraphs

This is my code:

Word.run(function (context) {
     var range = context.document.getSelection();
     var myContentControl = range.insertContentControl();
     myContentControl.title = "ZZZ";
     myContentControl.insertOoxml(dataXml, 'end');
     myContentControl.cannotEdit = false;
     myContentControl.cannotDelete = false;
     context.load(myContentControl, 'id, title, tag');
     return context.sync().then(function () {
          console.log("Success");
     });
});

This is the main part of dataXml content:

<?mso-application progid="Word.Document"?>
<pkg:package xmlns:pkg="http://schemas.microsoft.com/office/2006/xmlPackage">
  <!-- ... -->
  <pkg:part pkg:name="/word/document.xml" pkg:contentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml">
    <pkg:xmlData>
      <w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex" xmlns:cx1="http://schemas.microsoft.com/office/drawing/2015/9/8/chartex" xmlns:cx2="http://schemas.microsoft.com/office/drawing/2015/10/21/chartex" xmlns:cx3="http://schemas.microsoft.com/office/drawing/2016/5/9/chartex" xmlns:cx4="http://schemas.microsoft.com/office/drawing/2016/5/10/chartex" xmlns:cx5="http://schemas.microsoft.com/office/drawing/2016/5/11/chartex" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://purl.oclc.org/ooxml/officeDocument/relationships" xmlns:m="http://purl.oclc.org/ooxml/officeDocument/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://purl.oclc.org/ooxml/drawingml/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://purl.oclc.org/ooxml/wordprocessingml/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 wp14" w:conformance="strict">
        <w:body>
          <w:tbl>
            <w:tblPr>
              <w:tblStyle w:val="table" />
              <w:tblW w:w="95.25pt" w:type="dxa" />
              <w:tblInd w:w="0.8pt" w:type="dxa" />
              <w:tblCellMar>
                <w:top w:w="0pt" w:type="dxa" />
                <w:start w:w="0pt" w:type="dxa" />
                <w:bottom w:w="0pt" w:type="dxa" />
                <w:end w:w="0pt" w:type="dxa" />
              </w:tblCellMar>
              <w:tblLook w:firstColumn="1" w:firstRow="1" w:lastColumn="1" w:lastRow="1" w:noVBand="1" />
            </w:tblPr>
            <w:tblGrid>
              <w:gridCol w:w="952" />
              <w:gridCol w:w="953" />
            </w:tblGrid>
            <w:tr>
              <w:tblPrEx>
                <w:tblW w:w="95.25pt" w:type="dxa" />
                <w:tblInd w:w="0.8pt" w:type="dxa" />
                <w:tblCellMar>
                  <w:top w:w="0pt" w:type="dxa" />
                  <w:start w:w="0pt" w:type="dxa" />
                  <w:bottom w:w="0pt" w:type="dxa" />
                  <w:end w:w="0pt" w:type="dxa" />
                </w:tblCellMar>
                <w:tblLook w:firstColumn="1" w:firstRow="1" w:lastColumn="1" w:lastRow="1" w:noVBand="1" />
              </w:tblPrEx>
              <w:trPr>
                <w:trHeight w:hRule="exact" w:val="288" />
              </w:trPr>
              <w:tc>
                <w:tcPr>
                  <w:tcW w:w="48.75pt" w:type="dxa" />
                  <w:tcBorders>
                    <w:top w:val="single" w:sz="6" w:space="0" w:color="B6B6B6" />
                    <w:start w:val="single" w:sz="6" w:space="0" w:color="B6B6B6" />
                    <w:bottom w:val="single" w:sz="6" w:space="0" w:color="B6B6B6" />
                    <w:end w:val="single" w:sz="6" w:space="0" w:color="B6B6B6" />
                  </w:tcBorders>
                  <w:shd w:val="clear" w:color="auto" w:fill="FFFFFF" />
                  <w:noWrap />
                  <w:tcMar>
                    <w:top w:w="0.4pt" w:type="dxa" />
                    <w:start w:w="0.4pt" w:type="dxa" />
                    <w:bottom w:w="0.4pt" w:type="dxa" />
                    <w:end w:w="0.4pt" w:type="dxa" />
                  </w:tcMar>
                  <w:vAlign w:val="bottom" />
                  <w:hideMark />
                </w:tcPr>
                <w:p>
                  <w:pPr>
                    <w:rPr>
                      <w:rFonts w:ascii="Calibri" w:eastAsia="Calibri" w:hAnsi="Calibri" w:cs="Calibri" />
                      <w:b w:val="0" />
                      <w:bCs w:val="0" />
                      <w:i w:val="0" />
                      <w:iCs w:val="0" />
                      <w:smallCaps w:val="0" />
                      <w:color w:val="000000" />
                      <w:sz w:val="22" />
                      <w:szCs w:val="22" />
                    </w:rPr>
                  </w:pPr>
                  <w:r>
                    <w:rPr>
                      <w:rFonts w:ascii="Calibri" w:eastAsia="Calibri" w:hAnsi="Calibri" w:cs="Calibri" />
                      <w:b w:val="0" />
                      <w:bCs w:val="0" />
                      <w:i w:val="0" />
                      <w:iCs w:val="0" />
                      <w:smallCaps w:val="0" />
                      <w:color w:val="000000" />
                      <w:sz w:val="22" />
                      <w:szCs w:val="22" />
                    </w:rPr>
                    <w:t>A</w:t>
                  </w:r>
                </w:p>
              </w:tc>
              <w:tc>
                <w:tcPr>
                  <w:tcW w:w="48.75pt" w:type="dxa" />
                  <w:tcBorders>
                    <w:top w:val="single" w:sz="6" w:space="0" w:color="B6B6B6" />
                    <w:start w:val="single" w:sz="6" w:space="0" w:color="B6B6B6" />
                    <w:bottom w:val="single" w:sz="6" w:space="0" w:color="B6B6B6" />
                    <w:end w:val="single" w:sz="6" w:space="0" w:color="B6B6B6" />
                  </w:tcBorders>
                  <w:shd w:val="clear" w:color="auto" w:fill="FFFFFF" />
                  <w:noWrap />
                  <w:tcMar>
                    <w:top w:w="0.4pt" w:type="dxa" />
                    <w:start w:w="0.4pt" w:type="dxa" />
                    <w:bottom w:w="0.4pt" w:type="dxa" />
                    <w:end w:w="0.4pt" w:type="dxa" />
                  </w:tcMar>
                  <w:vAlign w:val="bottom" />
                  <w:hideMark />
                </w:tcPr>
                <w:p>
                  <w:pPr>
                    <w:rPr>
                      <w:rFonts w:ascii="Calibri" w:eastAsia="Calibri" w:hAnsi="Calibri" w:cs="Calibri" />
                      <w:b w:val="0" />
                      <w:bCs w:val="0" />
                      <w:i w:val="0" />
                      <w:iCs w:val="0" />
                      <w:smallCaps w:val="0" />
                      <w:color w:val="000000" />
                      <w:sz w:val="22" />
                      <w:szCs w:val="22" />
                    </w:rPr>
                  </w:pPr>
                  <w:r>
                    <w:rPr>
                      <w:rFonts w:ascii="Calibri" w:eastAsia="Calibri" w:hAnsi="Calibri" w:cs="Calibri" />
                      <w:b w:val="0" />
                      <w:bCs w:val="0" />
                      <w:i w:val="0" />
                      <w:iCs w:val="0" />
                      <w:smallCaps w:val="0" />
                      <w:color w:val="000000" />
                      <w:sz w:val="22" />
                      <w:szCs w:val="22" />
                    </w:rPr>
                    <w:t>B</w:t>
                  </w:r>
                </w:p>
              </w:tc>
            </w:tr>
            <w:tr>
              <w:tblPrEx>
                <w:tblW w:w="95.25pt" w:type="dxa" />
                <w:tblInd w:w="0.8pt" w:type="dxa" />
                <w:tblCellMar>
                  <w:top w:w="0pt" w:type="dxa" />
                  <w:start w:w="0pt" w:type="dxa" />
                  <w:bottom w:w="0pt" w:type="dxa" />
                  <w:end w:w="0pt" w:type="dxa" />
                </w:tblCellMar>
                <w:tblLook w:firstColumn="1" w:firstRow="1" w:lastColumn="1" w:lastRow="1" w:noVBand="1" />
              </w:tblPrEx>
              <w:trPr>
                <w:trHeight w:hRule="exact" w:val="288" />
              </w:trPr>
              <w:tc>
                <w:tcPr>
                  <w:tcW w:w="48.75pt" w:type="dxa" />
                  <w:tcBorders>
                    <w:top w:val="single" w:sz="6" w:space="0" w:color="B6B6B6" />
                    <w:start w:val="single" w:sz="6" w:space="0" w:color="B6B6B6" />
                    <w:bottom w:val="single" w:sz="6" w:space="0" w:color="B6B6B6" />
                    <w:end w:val="single" w:sz="6" w:space="0" w:color="B6B6B6" />
                  </w:tcBorders>
                  <w:shd w:val="clear" w:color="auto" w:fill="FFFFFF" />
                  <w:noWrap />
                  <w:tcMar>
                    <w:top w:w="0.4pt" w:type="dxa" />
                    <w:start w:w="0.4pt" w:type="dxa" />
                    <w:bottom w:w="0.4pt" w:type="dxa" />
                    <w:end w:w="0.4pt" w:type="dxa" />
                  </w:tcMar>
                  <w:vAlign w:val="bottom" />
                  <w:hideMark />
                </w:tcPr>
                <w:p>
                  <w:pPr>
                    <w:rPr>
                      <w:rFonts w:ascii="Calibri" w:eastAsia="Calibri" w:hAnsi="Calibri" w:cs="Calibri" />
                      <w:b w:val="0" />
                      <w:bCs w:val="0" />
                      <w:i w:val="0" />
                      <w:iCs w:val="0" />
                      <w:smallCaps w:val="0" />
                      <w:color w:val="000000" />
                      <w:sz w:val="22" />
                      <w:szCs w:val="22" />
                    </w:rPr>
                  </w:pPr>
                  <w:r>
                    <w:rPr>
                      <w:rFonts w:ascii="Calibri" w:eastAsia="Calibri" w:hAnsi="Calibri" w:cs="Calibri" />
                      <w:b w:val="0" />
                      <w:bCs w:val="0" />
                      <w:i w:val="0" />
                      <w:iCs w:val="0" />
                      <w:smallCaps w:val="0" />
                      <w:color w:val="000000" />
                      <w:sz w:val="22" />
                      <w:szCs w:val="22" />
                    </w:rPr>
                    <w:t>C</w:t>
                  </w:r>
                </w:p>
              </w:tc>
              <w:tc>
                <w:tcPr>
                  <w:tcW w:w="48.75pt" w:type="dxa" />
                  <w:tcBorders>
                    <w:top w:val="single" w:sz="6" w:space="0" w:color="B6B6B6" />
                    <w:start w:val="single" w:sz="6" w:space="0" w:color="B6B6B6" />
                    <w:bottom w:val="single" w:sz="6" w:space="0" w:color="B6B6B6" />
                    <w:end w:val="single" w:sz="6" w:space="0" w:color="B6B6B6" />
                  </w:tcBorders>
                  <w:shd w:val="clear" w:color="auto" w:fill="FFFFFF" />
                  <w:noWrap />
                  <w:tcMar>
                    <w:top w:w="0.4pt" w:type="dxa" />
                    <w:start w:w="0.4pt" w:type="dxa" />
                    <w:bottom w:w="0.4pt" w:type="dxa" />
                    <w:end w:w="0.4pt" w:type="dxa" />
                  </w:tcMar>
                  <w:vAlign w:val="bottom" />
                  <w:hideMark />
                </w:tcPr>
                <w:p>
                  <w:pPr>
                    <w:rPr>
                      <w:rFonts w:ascii="Calibri" w:eastAsia="Calibri" w:hAnsi="Calibri" w:cs="Calibri" />
                      <w:b w:val="0" />
                      <w:bCs w:val="0" />
                      <w:i w:val="0" />
                      <w:iCs w:val="0" />
                      <w:smallCaps w:val="0" />
                      <w:color w:val="000000" />
                      <w:sz w:val="22" />
                      <w:szCs w:val="22" />
                    </w:rPr>
                  </w:pPr>
                  <w:r>
                    <w:rPr>
                      <w:rFonts w:ascii="Calibri" w:eastAsia="Calibri" w:hAnsi="Calibri" w:cs="Calibri" />
                      <w:b w:val="0" />
                      <w:bCs w:val="0" />
                      <w:i w:val="0" />
                      <w:iCs w:val="0" />
                      <w:smallCaps w:val="0" />
                      <w:color w:val="000000" />
                      <w:sz w:val="22" />
                      <w:szCs w:val="22" />
                    </w:rPr>
                    <w:t>D</w:t>
                  </w:r>
                </w:p>
              </w:tc>
            </w:tr>
          </w:tbl>
          <w:p />
          <w:sectPr>
            <w:pgMar w:header="708" w:footer="708" />
            <w:cols w:space="708" />
          </w:sectPr>
        </w:body>
      </w:document>
    </pkg:xmlData>
  </pkg:part>
  <!-- ... -->
</pkg:package>

I manage to remove the first extra paragraph with this code:

var paragraphs = myContentControl.paragraphs;
context.load(paragraphs, 'items');
return context.sync().then(function () {
    paragraphs.items[0].delete();
    return context.sync().then(function () {
        console.log("Success");
    });
});

But if I try to remove the last paragraph as in the following line, it removes the content control and doesn't delete the extra paragraph.

paragraphs.items[paragraphs.items.length - 1].delete();

How can I delete the last extra paragraph using Office.js and without removing the content control?

Upvotes: 0

Views: 346

Answers (2)

EstevaoLuis
EstevaoLuis

Reputation: 2542

I found this workaround: insert directly to the document selection an Ooxml string containing also the content control and the table without paragraphs inside it.

So, this is the code:

Word.run(function (context) {
    context.document.getSelection().insertOoxml(dataXml, 'Start');
    return context.sync().then(function () {
        console.log("Success");
    });
});

This is the main part of dataXml content:

<pkg:package xmlns:pkg="http://schemas.microsoft.com/office/2006/xmlPackage">
  <!-- ... -->
  <pkg:xmlData>
      <w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
        <w:body>
          <w:sdt>
            <w:sdtPr>
              <w:alias w:val="ZZZ"/>
              <w:id w:val="0"/>
            </w:sdtPr>
            <w:sdtEndPr/>
            <w:sdtContent>
              <w:tbl>
                <!-- ... TABLE CONTENT ... -->
              </w:tbl>
            </w:sdtContent>
          </w:sdt>
        </w:body>
      </w:document>
    </pkg:xmlData>
  </pkg:part>
</pkg:package>

And this is the result:

Final result

At least the last paragraph is outside the content control.

Upvotes: 0

Cindy Meister
Cindy Meister

Reputation: 25663

In all "modern" versions of Word (modern = versions that support text wrapping / free positioning of tables on the page) a paragraph following the table is required.

The paragraph mark is visible in the Word Open XML in the question:

</w:tbl>
<w:p />
<w:sectPr>

So it will not be possible to delete this paragraph.

It should be possible, however, to format it with a very small font size (as I recall, Word supports .5 pt as the minimum size) with no Space Before or Space After.

Background as to why the paragraph mark is needed: Word stores the placement information of the table on the page, as well as some other structural information "in" the paragraph mark. We can't see it, but it's what's happening "under the covers". Very similar to a Shape object's Anchor.

Upvotes: 1

Related Questions