Office js Word JS api访问被拒绝(ContentControl.select、Body.getHtml)
我试图通过jsOffice js Word JS api访问被拒绝(ContentControl.select、Body.getHtml),office-js,office-addins,javascript-api-for-office,Office Js,Office Addins,Javascript Api For Office,我试图通过jsselect()函数选择内容控制--但我总是遇到访问被拒绝的错误: Error: { "name": "OfficeExtension.Error", "code": "AccessDenied", "message": "AccessDenied", "traceMessages": [], "debugInfo": { "errorLocation":"ContentControl.select" },
select()
函数选择内容控制--但我总是遇到访问被拒绝的错误:
Error:
{
"name": "OfficeExtension.Error",
"code": "AccessDenied",
"message": "AccessDenied",
"traceMessages": [],
"debugInfo":
{
"errorLocation":"ContentControl.select"
},
"stack":"AccessDenied: AccessDenied\n at Anonymous function (https://appsforoffice.microsoft.com/lib/1.1/hosted/word-win32-16.00.js:19:150094)\n at yi (https://appsforoffice.microsoft.com/lib/1.1/hosted/word-win32-16.00.js:19:163912)\n at st (https://appsforoffice.microsoft.com/lib/1.1/hosted/word-win32-16.00.js:19:163999)\n at d (https://appsforoffice.microsoft.com/lib/1.1/hosted/word-win32-16.00.js:19:163819)\n at c (https://appsforoffice.microsoft.com/lib/1.1/hosted/word-win32-16.00.js:19:162405)"
}
Debug info: {"errorLocation":"ContentControl.select"}
另外body.getHtml()
和body.getOoxml()
返回相同的拒绝访问错误
代码段:
// Run a batch operation against the Word object model.
Word.run(function (context) {
// Create a proxy object for the content controls collection.
var contentControls = context.document.contentControls;
// Queue a command to load the id property for all of the content controls.
context.load(contentControls, 'id');
// Synchronize the document state by executing the queued-up commands,
// and return a promise to indicate task completion.
return context.sync().then(function () {
if (contentControls.items.length === 0) {
console.log('No content control found.');
}
else {
// Queue a command to select the first content control.
contentControls.items[0].select();
// Synchronize the document state by executing the queued-up commands,
// and return a promise to indicate task completion.
return context.sync()
.then(function () {
console.log('Selected the first content control.');
});
}
});
})
.catch(function (error) {
console.log('Error: ' + JSON.stringify(error));
if (error instanceof OfficeExtension.Error) {
console.log('Debug info: ' + JSON.stringify(error.debugInfo));
}
});
首先,我认为我的代码可能有问题,但我也尝试过,同样的问题出现了。(尝试在本地和Internet上托管清单和应用程序)
有人在github中报告了相同的问题-github.com/OfficeDev/office js snippet explorer/issues/13
——但更改IE安全选项的解决方案并没有解决我的问题
我的Word版本-16.0.4266.1001卡米尔兹,你的版本很旧。我强烈建议您更新产品(转到文件->更新或按照说明操作)。目前最新版本为16.0.7870.2024。我刚刚运行了与您的示例完全相同的代码,并且成功了,所以这主要是一个过时的构建问题。 这就是说,您的场景可以概括为两行代码,只需访问主机一次,您应该始终尝试对此进行优化,特别是如果您希望您的代码对其他平台是最佳的。看看这个:
Word.run(函数(上下文){
context.document.contentControls.getFirstOrNullObject().select();
返回context.sync();
})