我正在尝试获取Word文档中所有内容控件的标题和顺序。以下代码有效:
function readContentControlsTitle() {
Word.run(function (context) {
var myDocParagraphs = context.document.body.paragraphs;
context.load(myDocParagraphs, 'text, outlineLevel');
return context.sync().then(function () {
for (var i = 0; i < myDocParagraphs.items.length; i++) {
var parContentControl = myDocParagraphs.items[i].parentContentControlOrNullObject;
context.load(parContentControl, 'title');
paragraphContentControls.push(parContentControl);
}
return context.sync().then(function () {
for (var iCount = 0; iCount < paragraphContentControls.length; iCount++) {
if (paragraphContentControls[iCount].title != null) {
// Some stuff with paragraphContentControls[iCount].title
}
}
return context.sync();
})
})
}).catch(function (error) {
if (error instanceof OfficeExtension.Error) {
console.log("Debug info: " + JSON.stringify(error.debugInfo));
}
});
}
当我在文档中插入目录时出现问题。在这种情况下,我总是在第二个context.sync中得到一个例外。如果我删除目录,它会再次运行。异常的输出是:
调试信息:{“code”:“GeneralException”,“message”:“GeneralException”,“errorLocation”:“Paragraph.parentContentControlOrNullObject”}
我的Word版本:16.0.7927.1020
答案 0 :(得分:1)
感谢您发现此问题。是的,这是一个错误。现在已经修好,应该在下个月发布。在修复之前,您可以将parentContentControlOrNullObject放入try / catch中。感谢。