How to add Customized comment in MS Word using OOXML?

295 Views Asked by At

I am creating a word custom add-in. In that user can able to add custom comments with color, bold, or highlight or set other possible options. But when I try to use directly <b>Hello This is comment</b> and also try with the below option. When the first option is to try it gives me RichApi.Error GeneralException: GeneralException. When I try to write this as a comment it prints as it is.

{\rtf1\ansi\ansicpg1252\deff0\nouicompat{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}{\f1\fswiss\fprq2\fcharset0 Microsoft Sans Serif;}{\f2\fnil\fcharset2 Symbol;}}\r\n{\colortbl ;\red0\green0\blue255;\red5\green99\blue193;}\r\n{\\generator Riched20 10.0.17763}\viewkind4\uc1 \r\n\pard\f0\fs17\lang1033 uses the parenthetical citation style, with most versions following format:\par\r\n\par\r\n\r\n\pard{\pntext\f2\'B7\tab}{\\pn\pnlvlblt\pnf2\pnindent0{\pntxtb\'B7}}... (Smith, 2005).\par\r\n{\pntext\f2\'B7\tab}Smith (2005) stated ...\par\r\n\r\n\pard\par\r\nThe Harvard system can vary, so please check your institution's style guide to confirm. \par\r\n\par\r\n\r\n\pard\widctlpar {\f1\fs16{\field{\*\fldinst{HYPERLINK "https://mysitehere"}}{\fldrslt{\ul\cf1\cf2\ul\lang2057 Click here}}}}\f1\fs16 for more information.\f0\fs17\par\r\n}\r\n

My Code:

range.insertOoxml(
    '<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/_rels/document.xml.rels" pkg:contentType="application/vnd.openxmlformats-package.relationships+xml" pkg:padding="256"><pkg:xmlData><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments" Target="comments.xml" xmlns="http://schemas.openxmlformats.org/package/2006/relationships" /></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:commentRangeStart w:id="0"/><w:r><w:t>' + selectedText + '</w:t></w:r><w:commentRangeEnd w:id="0"/><w:r><w:commentReference w:id="0"/></w:r></w:p></w:body></w:document></pkg:xmlData></pkg:part><pkg:part xmlns:pkg="http://schemas.microsoft.com/office/2006/xmlPackage" pkg:name="/word/comments.xml" pkg:contentType="application/vnd.openxmlformats-officedocument.wordprocessingml.comments+xml"><pkg:xmlData><w:comments xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:comment xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" w:id="0"><w:sdtContent><w:p><w:r><w:t>' + commentMessage + '</w:t></w:r></w:p></w:sdtContent></w:comment></w:comments></pkg:xmlData></pkg:part><pkg:part pkg:name="/word/_rels/comments.xml.rels" pkg:contentType="application/vnd.openxmlformats-package.relationships+xml"><pkg:xmlData><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"></Relationships></pkg:xmlData></pkg:part></pkg:package>',         
    "Replace"
);

If anyone has any idea how about this please let me know.

2

There are 2 best solutions below

4
On

You should be able to customize the comments you are inserting using Ooxml, just make sure to use the correct styling format to style the comments.

Change the following part of your Ooxml from this:

<w:comment xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" w:id="0">
  <w:sdtContent>
    <w:p>
      <w:r>
        <w:t>' + commentMessage + '</w:t>
      </w:r>
    </w:p>
  </w:sdtContent>
</w:comment>

to this:

<w:comment xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" w:id="0">
  <w:sdtContent>
    <w:p>
      <w:r>
        <w:t xml:space="preserve">This is a </w:t>
      </w:r>
      <w:r>
        <w:rPr>
          <w:b/>
          <w:bCs/>
        </w:rPr>
        <w:t>customized</w:t>
      </w:r>
      <w:r>
        <w:t xml:space="preserve"> comment.</w:t>
      </w:r>
    </w:p>
  </w:sdtContent>
</w:comment>

Notice the word "customized" will now be bold in the comment.

If you want to add further formatting, like highlighting, here is another sample:

<w:r>
  <w:t xml:space="preserve">This comment is </w:t>
</w:r>
<w:r>
  <w:rPr>
    <w:highlight w:val="yellow"/>
  </w:rPr>
  <w:t>highlighted</w:t>
</w:r>

You can learn more about Office Open XML styles here.

0
On

It depends a bit which styling you need but the WordAPI 1.4 supports some basic styling (like bold, italic, hyperlink).

https://learn.microsoft.com/en-us/javascript/api/word/word.commentcontentrange

const selection = context.document.getSelection();
const comment = selection.insertComment('hi!');
comment.contentRange.bold = true;

Highlight doesn't seem supported.