2017-09-15 82 views
0

我正在开发word的加载项。我试图用文本替换书签。(我最初的目标是在书签中插入文本,但有一个错误的API中所以这是另一种方法。Earlier question link使用office.js不能替换Word文档中的书签

这里是我的代码 -

Word.run(函数(上下文){

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

但它抛出exception.The错误跟踪消息是 - “GeneralException:GeneralException在An onymous函数(https://appsforoffice.microsoft.com/lib/beta/hosted/word-win32-16.01.js:21:211625)在AI(https://appsforoffice.microsoft.com/lib/beta/hosted/word-win32-16.01.js:21:248841) 在英尺(https://appsforoffice.microsoft.com/lib/beta/hosted/word-win32-16.01.js:21:248928) 在d(https://appsforoffice.microsoft.com/lib/beta/hosted/word-win32-16.01.js:21:248748) 在C(https://appsforoffice.microsoft.com/lib/beta/hosted/word-win32-16.01.js:21:247444)”

那么,有它的任何溶液或它是在API中另一个错误?

注意:我正在使用office.js API的1.4 beta版本。

+0

你可以使用发布的api版本吗? –

回答

1

您需要测试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) 
+0

是的。它不给null对象。我测试了你的代码,但没有运气。但它给出了同样的例外。 – reza

相关问题