1

I want to insert merge-field (<<xyz>>) from JavaScript API to the word sheet. But, I am not able to find JavaScript API for Office Word Add-in.

Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
Rakesh A R
  • 186
  • 1
  • 2
  • 17
  • Possible duplicate of [How do we add field code for Word using word javascript api](https://stackoverflow.com/questions/39826654/how-do-we-add-field-code-for-word-using-word-javascript-api) – Cindy Meister Jan 21 '19 at 15:34
  • This has already been asked before: https://stackoverflow.com/questions/39826654/how-do-we-add-field-code-for-word-using-word-javascript-api and https://stackoverflow.com/questions/50838137/how-to-insert-document-page-number-in-a-header-with-word-addin, for example. You may want to upvote the suggestion on UserVoice: https://officespdev.uservoice.com/forums/224641-feature-requests-and-feedback/suggestions/16473235-adding-field-codes-for-word-using-word-javascript – Cindy Meister Jan 21 '19 at 15:37
  • @CindyMeister I tried this link https://stackoverflow.com/questions/39826654/how-do-we-add-field-code-for-word-using-word-javascript-api but I am getting error for this xml . Is there any other xml for merge field, that I can try ? – Rakesh A R Jan 22 '19 at 15:05
  • 1
    Impossible to know/say without seeing your code. Note that the answer in the link is not for a merge field - it's a "pattern" for how fields can be inserted using Open XML (flat file OPC format). It might help you to create a very simple document containing a sample merge field. Save and close. Use the Open XML SDK Productivity Tool to open the document and inspect the underlying XML for the field. Compare that with the code in the linked answer to see how it differs / how that needs to be adjusted for your purposes. – Cindy Meister Jan 22 '19 at 15:35
  • 1
    You may also find this useful: http://www.ericwhite.com/blog/retrieving-fields-in-open-xml-wordprocessingml-documents/ – Cindy Meister Jan 22 '19 at 15:35
  • Yes !! got it @CindyMeister Thanks you – Rakesh A R Jan 23 '19 at 10:16
  • @CindyMeister How to create a stand alone office Add-in install able on Mac" which I can distribute as a single file to the users. ? without using office 365 . I referred to the below link but i didn't get solution. 1) https://learn.microsoft.com/en-us/office/dev/add-ins/publish/publish 2) https://learn.microsoft.com/en-us/office/dev/store/submit-to-the-office-store – Rakesh A R Jan 23 '19 at 14:34

2 Answers2

4
<?xml version=\"1.0\" standalone=\"yes\"?> <?mso-application progid=\"Word.Document\"?><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:p w:rsidR=\"002715FD\" w:rsidRDefault=\"002715FD\"><w:fldSimple w:instr=\" MERGEFIELD &quot;Last_Name&quot; \"><w:r><w:rPr><w:noProof /></w:rPr><w:t>«Last_Name»</w:t></w:r></w:fldSimple></w:p><w:sectPr w:rsidR=\"00000000\"> <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:sectPr> </w:body>  </w:document> </pkg:xmlData></pkg:part></pkg:package>  

Above is the xml for Merge Field

Word.run(function (context) {

    // Queue a command to get the current selection and then
    // create a proxy range object with the results.
    var range = context.document.getSelection();

    // Queue a commmand to insert OOXML in to the beginning of the range.
    range.insertOoxml("PASTE ABOVE MERGE XML", Word.InsertLocation.start);

    // Synchronize the document state by executing the queued commands,
    // and return a promise to indicate task completion.
    return context.sync().then(function () {
        console.log('OOXML added to the beginning of the range.');
    });
})
.catch(function (error) {
    console.log('Error: ' +JSON.stringify(error));
    if (error instanceof OfficeExtension.Error) {
        console.log('Debug info: ' + JSON.stringify(error.debugInfo));
    }
});
Rakesh A R
  • 186
  • 1
  • 2
  • 17
  • Not sure if this will help anyone but I found I had to change "«Last_Name»" to «Last_Name» to get the correct merge field brackets showing – Jaie Mar 03 '22 at 00:20
0

I had to create a tskpane addin on word using angular and the office js API, to insert merge fields on text and on shapes rectangle, using ooxml

  • text merge field xml template to insert into word

          <?mso-application progid="Word.Document"?>
          <w:wordDocument xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml" xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:aml="http://schemas.microsoft.com/aml/2001/core" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xml:space="preserve" w:embeddedObjPresent="no">
              <w:body>
                  <w:p>
                      <w:fldSimple w:instr=" MERGEFIELD &quot;${variableName}&quot; ">
                          <w:r>
                              <w:rPr>
                                  <w:noProof />
                              </w:rPr>
                              <w:t>«${variableName}»</w:t>
                          </w:r>
                      </w:fldSimple>
                  </w:p>
              </w:body>
          </w:wordDocument>
    
  • xml template for shape/rectangle with merge field

  <?mso-application progid="Word.Document"?>
  <w:wordDocument xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml" xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:aml="http://schemas.microsoft.com/aml/2001/core" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xml:space="preserve" w:embeddedObjPresent="no">
      <w:body>
          <w:p xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0">
              <w:r>
                  <w:pict>
                      <v:rect style="position:absolute;margin-left:0pt;margin-top:0pt;width:344pt;height:193.5pt;color:white;z-index:251659264;visibility:visible;mso-wrap-style:square;mso-width-percent:0;mso-height-percent:0;mso-wrap-distance-left:9pt;mso-wrap-distance-top:0;mso-wrap-distance-right:9pt;mso-wrap-distance-bottom:0;mso-position-horizontal:absolute;mso-position-horizontal-relative:text;mso-position-vertical:absolute;mso-position-vertical-relative:text;mso-width-percent:0;mso-height-percent:0;mso-width-relative:margin;mso-height-relative:margin;v-text-anchor:middle" fillcolor="#4472c4 [3204]" strokecolor="#1f3763 [1604]" strokeweight="1pt">
                          <v:textbox>
                              <w:txbxContent>
                                  <w:p>
                                      <w:pPr>
                                          <w:jc w:val="center" />
                                      </w:pPr>
                                      <w:fldSimple w:instr=" MERGEFIELD Image:${variableName} ">
                                          <w:r>
                                              <w:rPr>
                                                  <w:noProof />
                                              </w:rPr>
                                              <w:t>«Image:${variableName}»</w:t>
                                          </w:r>
                                      </w:fldSimple>
                                  </w:p>
                              </w:txbxContent>
                          </v:textbox>
                      </v:rect>
                  </w:pict>
              </w:r>
          </w:p>
      </w:body>
  </w:wordDocument>
  • ooxml insert method

    async insertMergeFieldOnDocument(xml: string) {
      return Word.run(async (context) => {
        let cursorOrSelection = context.document.getSelection();
        context.load(cursorOrSelection);
        await context.sync();
        cursorOrSelection.insertOoxml(xml, Word.InsertLocation.replace);
        await context.sync();
      });
    }
    
Aymen Boumaiza
  • 1,339
  • 1
  • 9
  • 9