将文本插入word文档时出错

问题描述 投票:0回答:1

我有一个带有代码的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().then没有被调用)。为了确保它不是Office API的一个疯狂问题,我直接在F12Chooser的控制台上从office-js-docs运行了这个示例,没有任何问题:

// 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-js word-addins
1个回答
1
投票

该代码在该特定框中不起作用的原因是因为它是Office 2016 MSI SKU(构建42xx)(而不是Office365即点击运行sku)。 MSI SKU有一些与有效insertLocations相关的错误,并且大多数错误都在API的1.2版本中得到修复。坏消息是1.2+版本没有向MSI构建向后移植。

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

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

© www.soinside.com 2019 - 2024. All rights reserved.