Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

office js - How to add Customized comment in MS Word using OOXML?

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.

{ tf1ansiansicpg1252deff0 ouicompat{fonttbl{f0fnilfcharset0 Microsoft Sans Serif;}{f1fswissfprq2fcharset0 Microsoft Sans Serif;}{f2fnilfcharset2 Symbol;}} {colortbl ; ed0green0lue255; ed5green99lue193;} {generator Riched20 10.0.17763}viewkind4uc1 pardf0fs17lang1033 uses the parenthetical citation style, with most versions following format:par par pard{pntextf2'B7ab}{pnpnlvlbltpnf2pnindent0{pntxtb'B7}}... (Smith, 2005).par {pntextf2'B7ab}Smith (2005) stated ...par pardpar The Harvard system can vary, so please check your institution's style guide to confirm. par par pardwidctlpar {f1fs16{field{*fldinst{HYPERLINK "https://mysitehere"}}{fldrslt{ulcf1cf2ullang2057 Click here}}}}f1fs16 for more information.f0fs17par }

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.

question from:https://stackoverflow.com/questions/65829989/how-to-add-customized-comment-in-ms-word-using-ooxml

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...