none
Inserting a Table into a Word doc with Range.InsertXML RRS feed

  • Question

  • Hello,

    I'm trying to use the Range.InsertXML to deal with the performance issues of manipulating tables via the Interop API's.  I cannot get the table to import withought the text having spacing after the text, i.e. 10px after a paragraph mark.

    I have tried setting the w:spacing element and have also tried using the w:pStyle and setting the style to 'No Spacing' and continue to have no luck.

    Anyone have any feedback of other things I can try?  Here's my XML fragment for the table I am inserting.  I am only showing the first row of the table for brevity.

    Thanks in advance.

    <w:wordDocument xmlns:w='http://schemas.microsoft.com/office/word/2003/wordml' xmlns:wx='http://schemas.microsoft.com/office/word/2003/auxHint' xmlns:wsp='http://schemas.microsoft.com/office/word/2003/wordml/sp2'>
      <w:tbl>
        <w:tblPr>
          <w:tblW w:w='5000' w:type='pct'/>
          <w:tblBorders>
            <w:top w:val='single' w:sz='4' wx:bdrwidth='10' w:space='0' w:color='595959'/>
            <w:left w:val='single' w:sz='4' wx:bdrwidth='10' w:space='0' w:color='595959'/>
            <w:bottom w:val='single' w:sz='4' wx:bdrwidth='10' w:space='0' w:color='595959'/>
            <w:right w:val='single' w:sz='4' wx:bdrwidth='10' w:space='0' w:color='595959'/>
            <w:insideH w:val='single' w:sz='4' wx:bdrwidth='10' w:space='0' w:color='595959'/>
            <w:insideV w:val='single' w:sz='4' wx:bdrwidth='10' w:space='0' w:color='595959'/>
          </w:tblBorders>
          <w:tblCellMar>
            <w:top w:w='43' w:type='dxa'/>
            <w:left w:w='115' w:type='dxa'/>
            <w:bottom w:w='29' w:type='dxa'/>
            <w:right w:w='115' w:type='dxa'/>
          </w:tblCellMar>
          <w:tblLook w:val='04A0'/>
        </w:tblPr>
        <w:tblGrid>
          <w:gridCol w:w='2448'/>
          <w:gridCol w:w='4550'/>
          <
    w:gridCol w:w='1296'/><w:gridCol w:w='1296'/>
        </w:tblGrid>
        <w:tr>
          <w:tc>
            <w:tcPr>
              <w:tcW w:w='2448' w:type='dxa'/>
              <w:tcBorders>
                <w:top w:val='single' w:sz='4' wx:bdrwidth='10' w:space='0' w:color='595959'/>
                <w:left w:val='single' w:sz='4' wx:bdrwidth='10' w:space='0' w:color='595959'/>
                <w:bottom w:val='single' w:sz='4' wx:bdrwidth='10' w:space='0' w:color='595959'/>
                <w:right w:val='single' w:sz='4' wx:bdrwidth='10' w:space='0' w:color='595959'/>
              </w:tcBorders>
              <w:shd w:val='clear' w:color='auto' w:fill='595959'/>
              <w:noWrap/>
            </w:tcPr>
            <w:p>
              <w:pPr>
                <w:pStyle w:val='NoSpacing'/>
                <w:spacing w:after='0'/>
                <w:sz w:val='20'/>
                <w:sz-cs w:val='20'/>
                <w:rPr>
                  <w:b/>
                  <w:color w:val='FFFFFF'/>
                </w:rPr>
              </w:pPr>
              <w:r>
                <w:rPr>
                  <w:b/>
                  <w:color w:val='FFFFFF'/>
                  <w:sz w:val='20'/>
                  <w:sz-cs w:val='20'/>
                </w:rPr>
                <w:t>Model</w:t>
              </w:r>
            </w:p>
          </w:tc>
          <w:tc>
            <w:tcPr>
              <w:tcW w:w='0' w:type='auto'/>
              <w:tcBorders>
                <w:top w:val='single' w:sz='4' wx:bdrwidth='10' w:space='0' w:color='595959'/><w:left w:val='single' w:sz='4' wx:bdrwidth='10' w:space='0' w:color='595959'/
                ><w:bottom w:val='single' w:sz='4' wx:bdrwidth='10' w:space='0' w:color='595959'/><w:right w:val='single' w:sz='4' wx:bdrwidth='10' w:space='0' w:color='595959'/>
              </w:tcBorders>
              <w:shd w:val='clear' w:color='auto' w:fill='595959'/>
              <w:noWrap/>
            </w:tcPr>
            <w:p>
              <w:pPr>
                <w:pStyle w:val='NoSpacing'/>
                <w:spacing w:after='0'/>
                <w:sz w:val='20'/>
                <w:sz-cs w:val='20'/>
                <w:rPr>
                  <w:b/>
                  <w:color w:val='FFFFFF'/>
                </w:rPr>
              </w:pPr>
              <w:r>
                <w:rPr>
                  <w:b/>
                  <w:color w:val='FFFFFF'/>
                  <w:sz w:val='20'/>
                  <w:sz-cs w:val='20'/>
                </w:rPr>
                <w:t>Description</w:t>
              </w:r>
            </w:p>
          </w:tc>
          <w:tc>
            <w:tcPr>
              <w:tcW w:w='1296' w:type='dxa'/>
              <w:tcBorders>
                <w:top w:val='single' w:sz='4' wx:bdrwidth='10' w:space='0' w:color='595959'/>
                <w:left w:val='single' w:sz='4' wx:bdrwidth='10' w:space='0' w:color='595959'/>
                <w:bottom w:val='single' w:sz='4' wx:bdrwidth='10' w:space='0' w:color='595959'/>
                <w:right w:val='single' w:sz='4' wx:bdrwidth='10' w:space='0' w:color='595959'/>
              </w:tcBorders>
              <w:shd w:val='clear' w:color='auto' w:fill='595959'/>
              <w:noWrap/>
            </w:tcPr>
            <w:p>
              <w:pPr>
                <w:pStyle w:val='NoSpacing'/>
                <w:spacing w:after='0'/>
                <w:sz w:val='20'/>
                <w:sz-cs w:val='20'/>
                <w:rPr>
                  <w:b/>
                  <w:color w:val='FFFFFF'/>
                </w:rPr>
              </w:pPr>
              <w:r>
                <w:rPr>
                  <w:b/>
                  <w:color w:val='FFFFFF'/>
                  <w:sz w:val='20'/>
                  <w:sz-cs w:val='20'/>
                </w:rPr>
                <w:t>MSRP</w:t>
              </w:r>
            </w:p>
          </w:tc>
          <w:tc>
            <w:tcPr>
              <w:tcW w:w='1296' w:type='dxa'/>
              <w:tcBorders>
                <w:top w:val='single' w:sz='4' wx:bdrwidth='10' w:space='0' w:color='595959'/>
                <w:left w:val='single' w:sz='4' wx:bdrwidth='10' w:space='0' w:color='595959'/>
                <w:bottom w:val='single' w:sz='4' wx:bdrwidth='10' w:space='0' w:color='595959'/>
                <w:right w:val='single' w:sz='4' wx:bdrwidth='10' w:space='0' w:color='595959'/>
              </w:tcBorders>
              <w:shd w:val='clear' w:color='auto' w:fill='595959'/>
              <w:noWrap/>
            </w:tcPr>
            <w:p>
              <w:pPr>
                <w:pStyle w:val='NoSpacing'/>
                <w:spacing w:after='0'/>
                <w:sz w:val='20'/>
                <w:sz-cs w:val='20'/>
                <w:rPr>
                  <w:b/>
                  <w:color w:val='FFFFFF'/>
                </w:rPr>
              </w:pPr>
              <w:r>
                <w:rPr>
                  <w:b/>
                  <w:color w:val='FFFFFF'/>
                  <w:sz w:val='20'/>
                  <w:sz-cs w:val='20'/>
                </w:rPr>
                <w:t>Price</w:t>
              </w:r>
            </w:p>
          </w:tc>
        </w:tr>
      </w:tbl>
    </w:wordDocument>
    

    Tuesday, August 28, 2012 11:41 PM

Answers

  • Hi Rob

    Yes, apply formatting to a table after-the-fact would give you a performance hit, no question.

    It should be possible to include the table style as part of the Word 2003 Word Processing XML you're inserting. Table styles were introduced in that version.

    If you select a formatted table, then use Selection.XML in a VBA macro you should be able to extract what Word wants you to use in your XML for InsertXML. Does that give you any clues?


    Cindy Meister, VSTO/Word MVP

    Friday, August 31, 2012 8:17 AM
    Moderator

All replies

  • Hi Rob

    The better place to ask this might be the Open XML SDK forum:
    http://social.msdn.microsoft.com/forums/en-US/oxmlsdk/threads/

    as the VSTO technology doesn't really deal with the Open XML file format. In any case, it would probably be better for me to move the discussion to the Word for Developers forum where there are more people who work with Word and might be able to provide ideas. Tell us if you want the discussion to be moved there...

    What's going on could be any number of things, but my best guess would be Paragraph SpaceAfter. You can test this by looking at such a table in the Word application. Select a cell (or row, or the table), on the Home tab, Paragraph group click the dialog launcher. Check the "Space After" setting (half-way down, on the left).


    Cindy Meister, VSTO/Word MVP

    Wednesday, August 29, 2012 7:09 AM
    Moderator
  • Thank you Cindy.  I will repost in the Word for Developers forum.  
    Wednesday, August 29, 2012 3:37 PM
  • Hello,

    I'm trying to use the Range.InsertXML to deal with the performance issues of manipulating tables via the Interop API's.  I cannot get the table to import withought the text having spacing after the text, i.e. 10px after a paragraph mark.

    I have tried setting the w:spacing element and have also tried using the w:pStyle and setting the style to 'No Spacing' and continue to have no luck.

    Anyone have any feedback of other things I can try?  Here's my XML fragment for the table I am inserting.  I am only showing the first row of the table for brevity.

    Thanks in advance.

    <w:wordDocument xmlns:w='http://schemas.microsoft.com/office/word/2003/wordml' xmlns:wx='http://schemas.microsoft.com/office/word/2003/auxHint' xmlns:wsp='http://schemas.microsoft.com/office/word/2003/wordml/sp2'>
      <w:tbl>
        <w:tblPr>
          <w:tblW w:w='5000' w:type='pct'/>
          <w:tblBorders>
            <w:top w:val='single' w:sz='4' wx:bdrwidth='10' w:space='0' w:color='595959'/>
            <w:left w:val='single' w:sz='4' wx:bdrwidth='10' w:space='0' w:color='595959'/>
            <w:bottom w:val='single' w:sz='4' wx:bdrwidth='10' w:space='0' w:color='595959'/>
            <w:right w:val='single' w:sz='4' wx:bdrwidth='10' w:space='0' w:color='595959'/>
            <w:insideH w:val='single' w:sz='4' wx:bdrwidth='10' w:space='0' w:color='595959'/>
            <w:insideV w:val='single' w:sz='4' wx:bdrwidth='10' w:space='0' w:color='595959'/>
          </w:tblBorders>
          <w:tblCellMar>
            <w:top w:w='43' w:type='dxa'/>
            <w:left w:w='115' w:type='dxa'/>
            <w:bottom w:w='29' w:type='dxa'/>
            <w:right w:w='115' w:type='dxa'/>
          </w:tblCellMar>
          <w:tblLook w:val='04A0'/>
        </w:tblPr>
        <w:tblGrid>
          <w:gridCol w:w='2448'/>
          <w:gridCol w:w='4550'/>
          <
    w:gridCol w:w='1296'/><w:gridCol w:w='1296'/>
        </w:tblGrid>
        <w:tr>
          <w:tc>
            <w:tcPr>
              <w:tcW w:w='2448' w:type='dxa'/>
              <w:tcBorders>
                <w:top w:val='single' w:sz='4' wx:bdrwidth='10' w:space='0' w:color='595959'/>
                <w:left w:val='single' w:sz='4' wx:bdrwidth='10' w:space='0' w:color='595959'/>
                <w:bottom w:val='single' w:sz='4' wx:bdrwidth='10' w:space='0' w:color='595959'/>
                <w:right w:val='single' w:sz='4' wx:bdrwidth='10' w:space='0' w:color='595959'/>
              </w:tcBorders>
              <w:shd w:val='clear' w:color='auto' w:fill='595959'/>
              <w:noWrap/>
            </w:tcPr>
            <w:p>
              <w:pPr>
                <w:pStyle w:val='NoSpacing'/>
                <w:spacing w:after='0'/>
                <w:sz w:val='20'/>
                <w:sz-cs w:val='20'/>
                <w:rPr>
                  <w:b/>
                  <w:color w:val='FFFFFF'/>
                </w:rPr>
              </w:pPr>
              <w:r>
                <w:rPr>
                  <w:b/>
                  <w:color w:val='FFFFFF'/>
                  <w:sz w:val='20'/>
                  <w:sz-cs w:val='20'/>
                </w:rPr>
                <w:t>Model</w:t>
              </w:r>
            </w:p>
          </w:tc>
          <w:tc>
            <w:tcPr>
              <w:tcW w:w='0' w:type='auto'/>
              <w:tcBorders>
                <w:top w:val='single' w:sz='4' wx:bdrwidth='10' w:space='0' w:color='595959'/><w:left w:val='single' w:sz='4' wx:bdrwidth='10' w:space='0' w:color='595959'/
                ><w:bottom w:val='single' w:sz='4' wx:bdrwidth='10' w:space='0' w:color='595959'/><w:right w:val='single' w:sz='4' wx:bdrwidth='10' w:space='0' w:color='595959'/>
              </w:tcBorders>
              <w:shd w:val='clear' w:color='auto' w:fill='595959'/>
              <w:noWrap/>
            </w:tcPr>
            <w:p>
              <w:pPr>
                <w:pStyle w:val='NoSpacing'/>
                <w:spacing w:after='0'/>
                <w:sz w:val='20'/>
                <w:sz-cs w:val='20'/>
                <w:rPr>
                  <w:b/>
                  <w:color w:val='FFFFFF'/>
                </w:rPr>
              </w:pPr>
              <w:r>
                <w:rPr>
                  <w:b/>
                  <w:color w:val='FFFFFF'/>
                  <w:sz w:val='20'/>
                  <w:sz-cs w:val='20'/>
                </w:rPr>
                <w:t>Description</w:t>
              </w:r>
            </w:p>
          </w:tc>
          <w:tc>
            <w:tcPr>
              <w:tcW w:w='1296' w:type='dxa'/>
              <w:tcBorders>
                <w:top w:val='single' w:sz='4' wx:bdrwidth='10' w:space='0' w:color='595959'/>
                <w:left w:val='single' w:sz='4' wx:bdrwidth='10' w:space='0' w:color='595959'/>
                <w:bottom w:val='single' w:sz='4' wx:bdrwidth='10' w:space='0' w:color='595959'/>
                <w:right w:val='single' w:sz='4' wx:bdrwidth='10' w:space='0' w:color='595959'/>
              </w:tcBorders>
              <w:shd w:val='clear' w:color='auto' w:fill='595959'/>
              <w:noWrap/>
            </w:tcPr>
            <w:p>
              <w:pPr>
                <w:pStyle w:val='NoSpacing'/>
                <w:spacing w:after='0'/>
                <w:sz w:val='20'/>
                <w:sz-cs w:val='20'/>
                <w:rPr>
                  <w:b/>
                  <w:color w:val='FFFFFF'/>
                </w:rPr>
              </w:pPr>
              <w:r>
                <w:rPr>
                  <w:b/>
                  <w:color w:val='FFFFFF'/>
                  <w:sz w:val='20'/>
                  <w:sz-cs w:val='20'/>
                </w:rPr>
                <w:t>MSRP</w:t>
              </w:r>
            </w:p>
          </w:tc>
          <w:tc>
            <w:tcPr>
              <w:tcW w:w='1296' w:type='dxa'/>
              <w:tcBorders>
                <w:top w:val='single' w:sz='4' wx:bdrwidth='10' w:space='0' w:color='595959'/>
                <w:left w:val='single' w:sz='4' wx:bdrwidth='10' w:space='0' w:color='595959'/>
                <w:bottom w:val='single' w:sz='4' wx:bdrwidth='10' w:space='0' w:color='595959'/>
                <w:right w:val='single' w:sz='4' wx:bdrwidth='10' w:space='0' w:color='595959'/>
              </w:tcBorders>
              <w:shd w:val='clear' w:color='auto' w:fill='595959'/>
              <w:noWrap/>
            </w:tcPr>
            <w:p>
              <w:pPr>
                <w:pStyle w:val='NoSpacing'/>
                <w:spacing w:after='0'/>
                <w:sz w:val='20'/>
                <w:sz-cs w:val='20'/>
                <w:rPr>
                  <w:b/>
                  <w:color w:val='FFFFFF'/>
                </w:rPr>
              </w:pPr>
              <w:r>
                <w:rPr>
                  <w:b/>
                  <w:color w:val='FFFFFF'/>
                  <w:sz w:val='20'/>
                  <w:sz-cs w:val='20'/>
                </w:rPr>
                <w:t>Price</w:t>
              </w:r>
            </w:p>
          </w:tc>
        </w:tr>
      </w:tbl>
    </w:wordDocument>

    Wednesday, August 29, 2012 3:41 PM
  • Hi Rob

    You did see my suggestion in the last paragraph of my first reply?


    Cindy Meister, VSTO/Word MVP

    Wednesday, August 29, 2012 4:45 PM
    Moderator
  • Hi Cindy,

    Yes, I did see your suggestion, but I believe that is what the <w:spacing w:after='0'/> tag should be doing.  Unfortunately, it doesn't seem to be working.

    I think I have come up with a work around solution which is to apply a table style to the table after it has been inserted.
     

    Wednesday, August 29, 2012 4:49 PM
  • Hi RobJohnson,

    I'm interesting with your issue. I tried to insert the xml snippet which you and Cindy provide to my document, however there will get exception "XML markup cannot be inserted in the specified location". Would you please tell me how to insert it to the Document?

    T.X.


    志异征诛,三让两家王朝
    功同开辟,一桮万古江南

    Thursday, August 30, 2012 6:47 AM
  • Hi Rob

    Yes, I was thinking a table style could be the solution, but I thought it would be better, perhaps, to track down what's causing the behavior. Shall we mark that as the Answer?

    By default, the Normal style will be applied to a new table. And the default setting for the Normal style in Word 2007/2010 does include Space After. That's why I was asking the question...


    Cindy Meister, VSTO/Word MVP

    Thursday, August 30, 2012 9:46 AM
    Moderator
  • Hi T.X.,

    I'm not sure what the problem with the XML markup could be that is causing the problem for you. Perhaps something was changed when I posed the XML to the forum.

    I've decided to work around the issue I was seeing by creating a table style and applying the style after the insertion of the table.

    Thursday, August 30, 2012 2:44 PM
  • Hi Cindy,

    Indeed I solved the issue by creating a table style and applying it after the insertion of the table.  I am seeing the the behavior that you are describing, that it is inheriting the Normal style with the Space After for the paragraph.  I did try setting the style of the table in the XML Markup but that had no effect.

    I did find that any formatting applied to text within the XML Markup for the table were kept when applying the table style, so this resolves my issue, although I think there is some performance loss during the application of the table style.

    I am working on some document level code in a product catalog that inserts tables of accessories related to the product.  This document may potentially have over 100 tables, so perfomance is key here there is functionality to "refresh" the product information (in essenece, rebuilding all of the tables).

    Thanks for your help.

    Thursday, August 30, 2012 2:50 PM
  • Hi RobJohnson,

    Would you please some me some snippet for your issue. It's very interesting.

    T.X.


    志异征诛,三让两家王朝
    功同开辟,一桮万古江南

    Friday, August 31, 2012 6:04 AM
  • Hi Rob

    Yes, apply formatting to a table after-the-fact would give you a performance hit, no question.

    It should be possible to include the table style as part of the Word 2003 Word Processing XML you're inserting. Table styles were introduced in that version.

    If you select a formatted table, then use Selection.XML in a VBA macro you should be able to extract what Word wants you to use in your XML for InsertXML. Does that give you any clues?


    Cindy Meister, VSTO/Word MVP

    Friday, August 31, 2012 8:17 AM
    Moderator
  • Hi Rob,

    Thanks for posting in the MSDN Forum.

    Do you have any update?

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, September 4, 2012 7:37 AM
    Moderator