Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ms word 在MS word Office.js加载项中实现键盘快捷键_Ms Word_Office Js - Fatal编程技术网

Ms word 在MS word Office.js加载项中实现键盘快捷键

Ms word 在MS word Office.js加载项中实现键盘快捷键,ms-word,office-js,Ms Word,Office Js,我试图在Word加载项中实现一个功能,跟踪用户在MS Word中键入的内容,然后执行自动替换。例如,用户可以键入 :smile: :frown: 我会自动将它们替换为 :) :( 我已经实现了一个处理程序来接收documentSelectionChanged事件,目前我正在使用以下事件处理程序: function refLabelShortcut() { var range, results, par; Word.run(function (context) { range

我试图在Word加载项中实现一个功能,跟踪用户在MS Word中键入的内容,然后执行自动替换。例如,用户可以键入

:smile: :frown:
我会自动将它们替换为

:) :(
我已经实现了一个处理程序来接收
documentSelectionChanged
事件,目前我正在使用以下事件处理程序:

function refLabelShortcut() {
  var range, results, 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!
      results = par.search('the*', {matchWildCards: true});
      //results = par.search('the');
      results.load('items'); // SOMETHING WRONG HERE?
      console.log('1');
    })
    .then(context.sync)
    .then(function() {
      console.log('2a'); // DOESN'T GET TO THIS LINE :(
      console.log(results.items.length);
      console.log('2b');
    });
  });
}
当搜索不是通配符时,上面的代码可以工作。我已经注释掉了非通配符搜索,所以您可以看到什么是有效的

执行通配符搜索时,控制台显示:

[text of the matching paragraph]
1
就这样。因此,代码不会到达最后一个
then
函数


知道为什么通配符搜索不起作用吗?
results.load('items')
行有错误吗?

范围。段落是一个集合对象,不能加载集合对象,因此
Range.load('parations')
不起任何作用。为了读取段落集合的项目,必须加载这些项目(或段落对象的某些属性)。尝试:


范围.段落.加载('items')

您能将其插入函数吗
range.insertText(“:smile:”,“:”)
@Mech,根据,它不是那样工作的。Hi Rich,我在第二个示例中又添加了两个
load
语句,但是
项在最后一个log语句中仍然没有加载。还有什么想法吗?删除行
range.load('段落')也执行行console.log(par.text);现在run?Rich,
console.log(par.text)
在你提出建议之前就一直对我有效。不起作用的部分是搜索段落<代码>结果。未加载项目
。我还删除了
context.load(结果)
。然后(context.sync())
应该是
。然后(context.sync)
。当我和我推荐的其他人一起进行更改时,您的代码对我来说是有效的。谢谢Rick,我已经解决了这个问题,但它仍然不能用于通配符搜索。我简化了我的问题,把重点放在最新的代码上。当使用通配符搜索运行当前代码时,是否在最后一个
then
函数中获得
console.log
语句?