[英]Replace text of current paragraph in MS word Office.js add-in
In a Word add-in, I'm trying to:在 Word 加载项中,我试图:
documentSelectionChanged
events,documentSelectionChanged
事件,foo
with the string bar
in the current paragraph.bar
替换字符串foo
。 Everything is working except the last part.除了最后一部分,一切都在工作。 The text of the Word document isn't changing.
Word 文档的文本没有改变。
This is my code:这是我的代码:
function updateText() {
var range, foo_range, par;
Word.run(function (context) {
range = context.document.getSelection();
range.paragraphs.load('items');
return context.sync()
.then(function() {
par = range.paragraphs.items[0];
console.log(par.text); // THIS WORKS!
foo_range = par.search('foo');
foo_range.load('items');
})
.then(context.sync)
.then(function() {
console.log(foo_range.items[0].text); // THIS WORKS!
foo_range.items[0].insertText('bar', 'Replace');
// Here, I am trying all the load options I can think of
foo_range.load('items');
foo_range.items[0].load('text');
foo_range.load('text');
range.paragraphs.load('items');
range.paragraphs.load('text');
return context.sync();
});
});
}
Any idea why foo
doesn't get replaced by bar
in the Word document?知道为什么
foo
没有被 Word 文档中的bar
替换吗?
I can't reproduce.我无法重现。 Your code works for me on desktop Office 365.
您的代码适用于桌面 Office 365。
BTW, none of those load
calls before the last context.sync
do anything, and you should delete them.顺便说一句,在最后一个
context.sync
之前的那些load
调用都没有做任何事情,你应该删除它们。 You only need to load a property (and then sync) when you are going to read the property after the sync.您只需要在同步后读取属性时加载属性(然后同步)。 Since you are only writing to the document, you don't need to load anything.
由于您只是在写入文档,因此您不需要加载任何内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.