我正在为办公室(word)开发一个addIn (Word),我一直在处理这个问题。我需要为将在新窗口/实例中打开的新文档分配自定义属性。
我已经对已经以这种方式打开的文档使用了自定义属性:
setProperty(propName, propValue) {
Word.run(context => {
context.document.properties.customProperties.add(propName, propValue);
return context.sync();
}).catch(error => {
if (error instanceof OfficeExtension.Error) {
console.log(
"Error code and message: " + JSON.stringify(error.debugInfo)
);
}
});
}
getFileOverride(attach, file) {
self.getDocumentAsBase64(attach.id).then(data => {
Word.run(context => {
let body = context.document.body;
body.insertFileFromBase64(data, Word.InsertLocation.replace);
return context
.sync()
.then(() => {
self.setProperty("fileId", file.id);
self.setProperty("attachId", attach.id);
})
.then(context.sync)
.catch(error => {
self.updateStatus("Error al obtener el archivo");
if (error instanceof OfficeExtension.Error) {
console.log(
"Error code and message: " + JSON.stringify(error.debugInfo)
);
}
});
}).catch(error => {
if (error instanceof OfficeExtension.Error) {
console.log(
"Error code and message: " + JSON.stringify(error.debugInfo)
);
}
});
});
}但是当我创建一个新的文档时,我不知道如何实现这一点。我尝试了以下方法,但是给出了一个一般的异常错误:
getDocumentAsBase64(function(data) {
Word.run(function(context) {
var myNewDoc = context.application.createDocument(data);
context.load(myNewDoc);
return context
.sync()
.then(function() {
myNewDoc.properties.load();
myNewDoc.properties.customProperties.add("custom", "prop");
myNewDoc.open();
})
.then(context.sync)
.catch(function(myError) {
//otherwise we handle the exception here!
updateStatus(myError.message);
});
}).catch(function(myError) {
updateStatus(myError.message);
});
});我尝试过创建一个类似于setProperty的函数,但它没有添加以下属性:
function setExternalProperty(document, propName, propValue) {
Word.run(context => {
document.properties.load();
document.properties.customProperties.add("custom", "prop");
return context.sync();
}).catch(error => {
if (error instanceof OfficeExtension.Error) {
console.log("Error code and message: " + JSON.stringify(error.debugInfo));
}
});
}我怎样才能做到这一点?
发布于 2018-05-10 17:11:25
我找到了解决办法,很简单。我将我的职能改为:
getFileNew(attach, file) {
self.getDocumentAsBase64(attach.id).then(data => {
Word.run(context => {
var myNewDoc = context.application.createDocument(data);
myNewDoc.properties.load();
myNewDoc.properties.customProperties.add("fileId", file.id);
myNewDoc.properties.customProperties.add("fileName", file.name);
myNewDoc.properties.customProperties.add("attachId", attach.id);
myNewDoc.properties.customProperties.add("attachName", attach.name);
myNewDoc.open();
return context.sync()
}).catch(error => {
if (error instanceof OfficeExtension.Error) {
console.log(
"Error code and message: " + JSON.stringify(error.debugInfo)
);
}
});
});
}SIDENOTE:这只适用于桌面版本的。如果要在Office的新窗口中打开文档,则必须省略customProperties,否则会引发异常
https://stackoverflow.com/questions/50275496
复制相似问题