Insert OOXML comment with track changes

北慕城南 提交于 2019-12-25 08:00:03

问题


In my Word add-in I am inserting comments by replacing the selected text with OOXML that contains the comment.

With "Track Changes" turned on Word registers this as 3 actions: delete+insert+comment. It even inserts a paragraph break but I am unsure if that is related.

Is there a way to only have it register as a comment action like when inserting a comment using Word functionality?

Using rangeObject.insertOoxml I tried to insert at the beginning and at the end of the string without any luck since the two OOXML inserts does not seem to relate (which makes sense):

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

    var preBody = '<?xml version="1.0" encoding="UTF-8"?><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" /></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>';
    var postBody = '</w:p></w:body></w:document></pkg:xmlData></pkg:part><pkg:part 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" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><w:comment w:id="0" w:author="Some User" w:date="2016-10-26T10:11:05" w:initials="SU"><w:p><w:r><w:t>My comment</w:t></w:r></w:p></w:comment></w:comments></pkg:xmlData></pkg:part></pkg:package>';

    var before = preBody + '<w:commentRangeStart w:id="0" />' + postBody;

    var afterBody = '<w:commentRangeEnd w:id="0" /><w:r><w:commentReference w:id="0" /></w:r>';
    var after = preBody + afterBody + postBody;

    range.insertOoxml(before, Word.InsertLocation.start);
    range.insertOoxml(after, Word.InsertLocation.end);
    return context.sync().then(function () {
        console.log('OOXML added to the beginning and the end 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));
    }
});

回答1:


This is a great question, thanks for asking it. When you use the insertOoxml method you are actually writing in the document's body, hence the behavior you observe when track changes is active, that's by design. There is no workaround for this, as of now. This issue will be solved when we support comments as a main feature of the API (and not inserting ooxml as workaround). Make sure to add your request or vote for an existing on in our uservoice! https://officespdev.uservoice.com/forums/224641-feature-requests-and-feedback/category/163566-add-in-word

Btw there was a bug we fixed a few months ago that the insertOoxml method was inserting a extra paragraph, make sure to update your Word to get the latest bug fixes. If this is still happening please share your Office Version number.



来源:https://stackoverflow.com/questions/40261291/insert-ooxml-comment-with-track-changes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!