在当前选择中插入wordMl(VSTO Word)

时间:2013-12-03 07:17:33

标签: c#-4.0 vsto openxml-sdk

我在当前的选择范围中插入wordMl

这里是我要插入的单词M1

<?mso-application progid="Word.Document"?>
<pkg:package xmlns:pkg="http://schemas.microsoft.com/office/2006/xmlPackage">
  <pkg:part pkg:name="/word/afchunk2.docx" pkg:contentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml" />
  <pkg:part pkg:name="/word/document.xml" pkg:contentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml">
    <pkg:xmlData>
      <w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
        <w:body>
          <w:p>
            <w:r>
              <w:t></w:t>
            </w:r>
          </w:p>
          <w:altChunk r:id="AltChunkId1" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" />
        </w:body>
      </w:document>
    </pkg:xmlData>
  </pkg:part>
  <pkg:part pkg:name="/word/_rels/document.xml.rels" pkg:contentType="application/vnd.openxmlformats-package.relationships+xml">
    <pkg:xmlData>
      <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
        <Relationship Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/aFChunk" Target="/word/afchunk2.docx" Id="AltChunkId1" />
      </Relationships>
    </pkg:xmlData>
  </pkg:part>
  <pkg:part pkg:name="/_rels/.rels" pkg:contentType="application/vnd.openxmlformats-package.relationships+xml">
    <pkg:xmlData>
      <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
        <Relationship Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="/word/document.xml" Id="R1ff464e618a64ac8" />
      </Relationships>
    </pkg:xmlData>
  </pkg:part>
</pkg:package>

但是当我使用

插入它时

range.insertXml方法它抛出以下异常消息

“无法在指定位置插入XML标记。”

我使用openXml sdk创建了文档。

1 个答案:

答案 0 :(得分:0)

您无法直接添加使用OpenXML SDK创建的XML。您需要将文档包(WordprocessingDocument)转换为Flat OPC Format,这是range.InsertXml期望的格式。

更多信息: http://blogs.msdn.com/b/ericwhite/archive/2008/09/29/the-flat-opc-format.aspx