2

我正在为 Word for mac 创建一个 MS-Word 加载项,我必须创建一个带有任务窗格的自定义评论部分,因此我创建了一个文本框并在单击按钮时创建一个按钮评论添加到该选定文本。

我找到了很多文章,但对我不起作用,因此我在下面附上了示例代码。

HTML 文件

    <div class="padding">
            <textarea id="areaDiv"></textarea>
            <button id="addComment">Add Comment</button>
            <div id="errorDiv"></div>
     </div>

**JS File**
Office.onReady(function () {
    // Office is ready
    $(document).ready(function () {
        // The document is ready
        // Use this to check whether the API is supported in the Word client.
        if (Office.context.requirements.isSetSupported('WordApi', '1.1')) {
            // Do something that is only available via the new APIs
            $('#addComment').click(addComment);

            $('#supportedVersion').html('This code is using Word 2016 or later.');
        }
        else {
            // Just letting you know that this code will not work with your version of Word.
            $('#supportedVersion').html('This code requires Word 2016 or later.');
        }
    });
});


// Function that writes to a div with id='message' on the page.

function addComment() {
    Office.context.document.getSelectedDataAsync(Office.CoercionType.Text,
        { valueFormat: "unformatted", filterType: "all" },
        function (asyncResult) {
            var error = asyncResult.error;
            if (asyncResult.status === Office.AsyncResultStatus.Failed) {
                write('');
            }
            else {
                var range = asyncResult.value;
                write(range);
            }
        });
}
function onError(error) {
    $('#errorDiv').text(error.name + ' ' + error.code + ': ' + error.message);
}
function write(range) {
    $('#errorDiv').text(range);
    var text = $("#areaDiv").val();
    if (range != null && range != "" && text != null && text != "") {
        $('#errorDiv').text(range + " " + text);
        var document = Office.context.document.properties.comments;
        document.add(range, text);
        $("#areaDiv").val("");
    }
}

这里没有错误,但是没有注释的问题是设置选定的文本并且没有进入 catch 块。

如果有人对此有任何想法,那么它对我有很大帮助。

4

0 回答 0