插入带有跟踪更改的 OOXML 注释

Insert OOXML comment with track changes

在我的 Word 加载项中,我通过将所选文本替换为包含评论的 OOXML 来插入评论。

打开 "Track Changes" Word 将其注册为 3 个操作:删除+插入+注释。它甚至插入了一个段落分隔符,但我不确定这是否相关。

有没有办法像在使用 Word 功能插入评论时那样只注册为评论操作?

使用 rangeObject.insertOoxml 我尝试在字符串的开头和结尾插入但没有任何运气,因为两个 OOXML 插入似乎不相关(这是有道理的):

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));
    }
});

这是一个很好的问题,感谢您提出这个问题。当您使用 insertOoxml 方法时,您实际上是在文档正文中 编写 ,因此您在跟踪更改处于活动状态时观察到的行为是设计使然。目前还没有解决方法。当我们支持将评论作为 API 的主要功能(而不是插入 ooxml 作为解决方法)时,这个问题将得到解决。确保在我们的用户语音中添加您的请求或投票给现有的! https://officespdev.uservoice.com/forums/224641-feature-requests-and-feedback/category/163566-add-in-word

顺便说一句,几个月前我们修复了一个错误,即 insertOoxml 方法插入了一个额外的段落,请确保更新您的 Word 以获得最新的错误修复。如果这种情况仍然存在,请分享您的 Office 版本号。