Word JS api访问被拒绝(ContentControl.select,Body.getHtml) [英] Word JS api Access Denied (ContentControl.select, Body.getHtml)

查看:136
本文介绍了Word JS api访问被拒绝(ContentControl.select,Body.getHtml)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过js select()函数选择内容控制-

I'm trying to select content control through js select() function - docs - but I'm always getting Access Denied error:

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()返回相同的访问被拒绝错误.

Also body.getHtml() and body.getOoxml() returns the same Access Denied error.

代码段:

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

首先,我认为我的代码可能有问题,但我也尝试过 snipper-explorer ,并且出现了相同的问题. (尝试在本地和Internet上托管清单和应用)

Firstly I thought it might be something wrong with my code but I've also tried snipper-explorer and the same problem appeared. (tried hosting manifest & app both locally and over the Internet)

有人在github中报告了相同的问题-github.com/OfficeDev/office-js-snippet-explorer/issues/13-但是更改IE安全选项的解决方案无法解决我的情况.

Someone reported the same issue in github - github.com/OfficeDev/office-js-snippet-explorer/issues/13 - but solution with changing IE security options didn't solve my case.

我的Word版本-16.0.4266.1001

My Word version - 16.0.4266.1001

推荐答案

Kamilz,您使用的是旧版本.我强烈建议您更新产品(转到文件->更新或遵循此处).现在的最新版本是16.0.7870.2024.我只是完全按照您的示例运行了您的代码并成功了,所以这主要是一个过时的构建问题. 就是说,您的方案可以归纳为2行代码,并且仅需经过1次主机访问,您就应该始终尝试对其进行优化,特别是如果您希望您的代码对于其他平台而言是最佳的.检查一下:

Kamilz, you are in a quite old build. I strongly suggest you to update your product (goto file->update or follow the instructions here). Right now the latest build is 16.0.7870.2024. I just ran your code exactly as in your example and was successful, so this is mostly an outdated build issue. That said, your scenario can be summarized to 2 lines of code, and just 1 trip to the host, you should always try to optimize this, specially if you want your code to be optimal for other platforms. Check this out:

 Word.run(function (context) { 
        context.document.contentControls.getFirstOrNullObject().select();
        return context.sync();

    })

好的,它应该和上面的代码完全一样. 希望这会有所帮助!

ok that should do exactly the same as your code above. hope this helps!

这篇关于Word JS api访问被拒绝(ContentControl.select,Body.getHtml)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆