Office js 将文本插入word文档时出错

Office js 将文本插入word文档时出错,office-js,word-addins,Office Js,Word Addins,我有一个MS Word加载项,其代码用于在选择后插入文本: Word.run(function (context) { var selection = context.document.getSelection(); selection.insertText(text, Word.InsertLocation.after).select(); return context.sync().then(function () {

我有一个MS Word加载项,其代码用于在选择后插入文本:

Word.run(function (context) {

    var selection = context.document.getSelection();           
    selection.insertText(text, Word.InsertLocation.after).select();

    return context.sync().then(function () {      
        console.log('Success');
    });

});
这段代码已经在许多不同的机器上进行了测试,没有出现错误。然而,令我惊讶的是,我们的一位客户最近报告说,他的文档中根本没有添加任何文本

在一些调试之后,我注意到消息
Success
从未打印到控制台(
context.sync()。然后,
没有被调用)。为了确保Office API没有什么疯狂的问题,我在F12Chooser的控制台上直接从Office js文档中运行了这个示例,但没有任何问题:

// Run a batch operation against the Word JavaScript API.
Word.run(function (context) {

    // Create a proxy object for the document body.
    var body = context.document.body;

    // Queue a command to load the text property of the proxy body object.
    context.load(body, 'text');

    // Queue a command to insert text into the end of the Word document body.
    body.insertText('This is text inserted after loading the body.text property',
                    Word.InsertLocation.end);

    // Synchronize the document state by executing the queued commands,
    // and return a promise to indicate task completion.
    return context.sync().then(function () {
        console.log("Body contents: " + body.text);
    });
})
(文本插入文件末尾)

同样,我的代码在许多不同的机器和设置中运行,似乎只有在这个人的机器中才有问题。他正在使用:

Office Professional Plus 2016
Microsoft Word 2016 MSO (16.0.4266.1001) 64 Bits
Windows 10 Pro
Internet Explorer 11.413.15063

有人能帮我吗?谢谢

该代码在该特定框中不起作用的原因是因为它是Office 2016 MSI SKU(builds 42xx)(而不是Office 365又名单击运行SKU)。MSI SKU有一些与有效InsertLocation相关的bug,其中大多数bug在API的1.2版本中已修复。坏消息是,1.2+版本没有向后移植到MSI版本

请尝试insertLocation.end(而不是insertLocation.after),我认为您的代码应该可以工作(行为略有不同)


或者,如果您的客户迁移到O365,问题将立即得到解决

其工作的计算机是否也使用Word 2016 MSO(16.0.4266.1001)64位?是否可以在单词的末尾添加一个catch.run。catch(function(error){console.log(“error:+error”);if(error instanceof OfficeExtension.error){console.log(“Debug info:+JSON.stringify(error.debugInfo));}})参见本文的步骤7了解如何做到这一点:非常感谢。我的解决方案将是一种捕获(根据Rick Kirkham的评论)和另一种逻辑(可能是insertLocation.end)的混合。顺致敬意,