Search code examples
ms-wordoffice365openxmloffice-js

Insert comment in Word using office.js


I am trying to make a Word add-in in office.js that inserts comments in the document. It seems to me the only way to achieve this is to use OOXML.

I can insert the comment but my problem is that when I do so a paragraph break is inserted as well as can be seen from this image.

enter image description here

A far as I can see it boils down to that if I am only inserting some text the content of the body looks like the following which works fine

<w:p>
    <w:r>
        <w:t>Some text</w:t>
    </w:r>
</w:p>

But if I am inserting a reference to a comment it results in a paragraph end right after whatever I am insering. In that case the content of the body looks like this:

<w:p>
    <w:commentRangeStart w:id="0"/>
    <w:r>
        <w:t>selectedText</w:t>
    </w:r>
    <w:r>
        <w:commentReference w:id="0"/>
    </w:r>
    <w:commentRangeEnd w:id="0"/>
</w:p>

The javascript code used to replace the highlighted text looks like this:

function insertComment() {
    Office.context.document.getSelectedDataAsync(
        Office.CoercionType.Text,
        function (result) {
            if (result.status == "succeeded") {
                // Get the OOXML returned from the getSelectedDataAsync call.
                var selectedText = result.value;
                var comment = getCommentAsOoxml(selectedText);
                Office.context.document.setSelectedDataAsync(comment, { coercionType: Office.CoercionType.Ooxml }, function (asyncResult) {
                    if (asyncResult.status == "failed") {
                        console.debug("Action failed with error: " + asyncResult.error.message);
                    }
                });
            }
        });
}

The OOXML that is inserted can be seen here:

<?xml version="1.0" encoding="UTF-8" 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/_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>
                        <w:commentRangeStart w:id="0"/>
                        <w:r>
                            <w:t>selectedText</w:t>
                        </w:r>
                        <w:r>
                            <w:commentReference w:id="0"/>
                        </w:r>
                        <w:commentRangeEnd w:id="0"/>
                    </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">
                <w:comment w:id="0" w:author="jkh" w:date="2016-04-27T08:15:00Z" w:initials="JH">
                    <w:p>
                        <w:r>
                            <w:t>comment</w:t>
                        </w:r>
                    </w:p>
                </w:comment>
            </w:comments>
        </pkg:xmlData>
    </pkg:part>
</pkg:package>

Sorry for the exceptionally long post. A new user is regretfully restricted in inserting links and images :(


Solution

  • this is actually a confirmed bug in the API. A fix for this will be rolled out as part of an upcoming Office update.