【发布时间】:2018-03-02 23:08:12
【问题描述】:
我有一个 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
谁能帮我解决这个问题?谢谢!
【问题讨论】:
-
运行它的计算机是否也使用 Word 2016 MSO (16.0.4266.1001) 64 位?是否可以在 Word.run 的末尾添加一个捕获? .catch(function (error) { console.log("Error: " + error); if (error instanceof OfficeExtension.Error) { console.log("Debug info: " + JSON.stringify(error.debugInfo)); } }) 请参阅本文的第 7 步了解如何执行此操作:docs.microsoft.com/en-us/office/dev/add-ins/tutorials/…
标签: office-js word-addins