1

I have the following:

  Word.run(function (context) {
    docSelection = context.document.getSelection();

var text = '<w:sdt><w:sdtPr><w:alias w:val="SomeAlias" /><w:tag w:val="SomeTag" /><w:lock w:val="sdtLocked" /></w:sdtPr>'
 + '<w:sdtContent><w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:pPr><w:pStyle w:val="NormalWeb" /></w:pPr>' 
 + '<w:r><w:t>MY TEXT</w:t></w:r></w:p></w:sdtContent></w:sdt>';

    docSelection.insertOoxml(text, "Replace");  
      return context.sync().then(function () {
        console.log('Text insertion complete');
      });
    })
  .catch(function (error) {
      console.log("Error: " + error);
      if (error instanceof OfficeExtension.Error) {
          console.log("Debug info: " + JSON.stringify(error.debugInfo));
          displayError(JSON.stringify(error));
          return false;
      }
  });

Running this, I get the error: "Debug info: {"code":"GeneralException","message":"GeneralException","errorLocation":"Range.insertOoxml","statement":"var insertOoxml = selection.insertOoxml(...);","surroundingStatements":["var root = context.root;","var selection = root.getSelection();","// Instantiate {selection}","// >>>>>","var insertOoxml = selection.insertOoxml(...);","// <<<<<","// Instantiate {insertOoxml}"],"fullStatements":["var root = context.root;","var selection = root.getSelection();","// Instantiate {selection}","var insertOoxml = selection.insertOoxml("<w:sdt><w:sdtPr><w:alias w:val=\"SomeAlias\" /><w:tag w:val=\"SomeTag\" /><w:lock w:val=\"sdtLocked\" /></w:sdtPr><w:sdtContent><w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\"><w:pPr><w:pStyle w:val=\"NormalWeb\" /></w:pPr><w:r><w:t>MY TEXT</w:t></w:r></w:p></w:sdtContent></w:sdt>", "Replace");","// Instantiate {insertOoxml}"]}"

which I quite simply don't understand.

Inserting with docSelection.insertOoxml("<w:p xmlns:w='http://schemas.microsoft.com/office/word/2003/wordml'><w:r><w:t>TEST</w:t></w:r></w:p>", "Replace");

works fine so the rest of my code is OK.

Any ideas what I'm doing wrong?

1 Answers1

0

XML:

<pkg:package xmlns:pkg='http://schemas.microsoft.com/office/2006/xmlPackage'>
            <pkg:part pkg:name='/_rels/.rels' pkg:contentType='application/vnd.openxmlformats-package.relationships+xml' pkg:padding='512'>
                <pkg:xmlData>
                    <Relationships xmlns='http://schemas.openxmlformats.org/package/2006/relationships'>
                        <Relationship Id='rId1' Type='http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument' Target='word/document.xml'/>
                    </Relationships>
                </pkg:xmlData>
            </pkg:part>
            <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:sdt>
                                <w:sdtPr>
                                    <w:lock w:val="contentLocked"/>
                                </w:sdtPr>
                                <w:sdtContent>
                                    <w:p>
                                        <w:pPr>
                                            <w:alias w:val="Table of Contents"/>
                                            <w:tag w:val="TOCLitera"/>
                                            <w:spacing w:after="480"/>
                                            <w:jc w:val="center"/>
                                            <w:pStyle w:val='TOCHeading'/>
                                        </w:pPr>
                                        <w:r>
                                            <w:rPr>
                                                <w:keepNext/>
                                                <w:keepLines/>
                                                <w:rFonts w:asciiTheme="majorHAnsi" w:eastAsiaTheme="majorEastAsia" w:hAnsiTheme="majorHAnsi" w:cstheme="majorBidi"/>
                                                <w:sz w:val="32"/>
                                                <w:szCs w:val="32"/>
                                                <w:b/>
                                                <w:caps/>
                                            </w:rPr>
                                            <w:t>Table of Contents</w:t>
                                        </w:r>
                                    </w:p>  
                                    <w:r>
                    <w:rPr>
                        <w:b/>
                        <w:bCs/>
                        <w:noProof/>
                    </w:rPr>
                    <w:t>No table of contents entries found.</w:t>
                </w:r>
                                </w:sdtContent>
                            </w:sdt>
                        </w:body>
                    </w:document>
                </pkg:xmlData>
            </pkg:part>
        </pkg:package>

Below I gave link example to create TOC:

Link of TOC

Sagar Mistry
  • 131
  • 11