Ms word Office JS-将customProperty添加到新文档
我正在为office开发一个插件(word),我一直在解决这个问题。我需要为将在新窗口/实例中打开的新文档分配自定义属性 我已经在为已经以这种方式打开的文档使用自定义属性:Ms word Office JS-将customProperty添加到新文档,ms-word,ms-office,office-js,Ms Word,Ms Office,Office Js,我正在为office开发一个插件(word),我一直在解决这个问题。我需要为将在新窗口/实例中打开的新文档分配自定义属性 我已经在为已经以这种方式打开的文档使用自定义属性: setProperty(propName, propValue) { Word.run(context => { context.document.properties.customProperties.add(propName, propValue); return context.sync(); }).c
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)
);
}
});
});
}
但是当我创建一个新文档时,我不知道如何实现这一点。我尝试了以下方法,但给出了一个一般异常错误:
我尝试过创建一个类似于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));
}
});
}
我怎样才能做到这一点呢?我找到了解决方案,非常简单。我将我的功能更改为:
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)
);
}
});
});
}
旁注:这仅适用于桌面版。如果您想在Office Online的新窗口中打开文档,您必须忽略customProperties,否则它将引发异常请参阅此问题的答案:-这是相同的问题。这是精心设计的。可能是@Cindymister的副本。请参阅我的答案。确实有可能。我发布了解决方案
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)
);
}
});
});
}