Office js 无法使用office.js替换word文档中的书签

Office js 无法使用office.js替换word文档中的书签,office-js,Office Js,我正在为word开发一个外接程序。我正在尝试用文本替换书签。(我最初的目标是在书签中插入文本,但API中有一个bug,所以这是另一种方法。) 这是我的密码: Word.run(function (context) { var doc = context.document; //get the bookmark range object by its name var bookmarkRange=doc.getBookmarkRangeOrNullObject("csc

我正在为word开发一个外接程序。我正在尝试用文本替换书签。(我最初的目标是在书签中插入文本,但API中有一个bug,所以这是另一种方法。)

这是我的密码:

Word.run(function (context) {

    var doc = context.document;

    //get the bookmark range object by its name
    var bookmarkRange=doc.getBookmarkRangeOrNullObject("cscasenumber01");

    //insert a data and replace thee bookmark range
    bookmarkRange.insertText("test data",Word.InsertLocation.replace);

    // Synchronize the document state by executing the queued commands, 
    return context.sync();

}).catch(errorHandler);
但它抛出了一个例外。错误跟踪消息为:

GeneralException:匿名函数的GeneralException () 在ai () 英国《金融时报》 () 在d () 在c ()”

那么有什么解决方案吗?或者它是API中的另一个bug


注意:我使用的是office.js API的1.4测试版。

您需要测试bookmarkRange是否为空对象。请尝试以下代码:

var bookmarkRange=doc.getBookmarkRangeOrNullObject("cscasenumber01");
bookmarkRange.load();

return context.sync()
.then(function() {
   if (bookmarkRange.isNullObject) {
        // handle case of null object here
   } else {
        bookmarkRange.insertText("test data",Word.InsertLocation.replace);
   }
})
.then(context.sync)

对。它没有给出空对象。我已经测试了你的代码,但没有运气。它仍然给出相同的异常。你能使用api的发布版本吗?