我已经为word创建了一个taskpane addin,它使用Document.getFileAsync方法以压缩格式(docx)获取文档内容 . 这适用于.docx文件,但如果使用旧的.doc文件,则无意中失败 .
我收到以下错误:
code: 5001
message: "An internal error has occurred."
name: "Internal Error"
有没有办法在调用getFileAsync之前检测无效格式的文档?我已尝试使用以下代码读取文档属性格式值:
return Word.run(function (context) {
var properties = context.document.properties;
context.load(properties, "format");
return context.sync()
.then(function () {
return properties.format;
});
});
但是对于docx和doc文件,返回的值始终是空字符串 .
我希望能够检测旧的文件格式,以便我可以向用户显示适当的错误消息 .
1 回答
getFileAsync()
方法仅适用于.docx
文件 . 只是为了检测正确的文件,您只需检查文件的扩展名:fname.substr((~-fname.lastIndexOf('.') >>> 0) + 2)
其中fname
是文件名 . 并相应地提示您的消息 .