Nanji Mange
Nanji Mange

Reputation: 2265

How to add comment to selected text in MS Word using Office App?

I am new in Office App development. I want to add comment to selected text on button click. I can get selected text using following code but I don't know how to add comment in selected text.

Code:Home.js

(function () {
    "use strict";

    // The initialize function must be run each time a new page is loaded
    Office.initialize = function (reason) {
        $(document).ready(function () {
            app.initialize();

            $('#get-data-from-selection').click(getDataFromSelection);
        });
    };

    // Reads data from current document selection and displays a notification
    function getDataFromSelection() {
        Office.context.document.getSelectedDataAsync(Office.CoercionType.Text,
            function (result) {
                if (result.status === Office.AsyncResultStatus.Succeeded) {
                    app.showNotification('The selected text is:', '"' + result.value + '"');
                } else {
                    app.showNotification('Error:', result.error.message);
                }
            }
        );
    }
})();

SS: enter image description here

Can anybody please guide me about to add comment in selected text?

Upvotes: 0

Views: 685

Answers (2)

Shiv Yadav
Shiv Yadav

Reputation: 311

const commentTitle = "Hello Comment";
let comment = context.document.getSelection().insertComment(commentTitle);
await context.sync();

Upvotes: 0

Mikhail Barycheuski
Mikhail Barycheuski

Reputation: 119

I can use the setSelectedDataAsync method with the options. https://dev.office.com/reference/add-ins/shared/customxmlnodetype-enumeration

   Office.context.document.setSelectedDataAsync("my comment", {CustomXMLNodeType: Office.Office.CustomXMLNodeType.NodeComment}
function (asyncResult) {
    var error = asyncResult.error;
    if (asyncResult.status === Office.AsyncResultStatus.Failed){
        console.log(error.name + ": " + error.message);
    }
});

Upvotes: 0

Related Questions