问题描述
我仔细阅读了本文档,并遵循了此处提到的示例,但无法使用文件名保存文档。 https://docs.microsoft.com/en-gb/javascript/api/word/word.document?redirectedfrom=MSDN&view=word-js-preview#save-- 例如
// Run a batch operation against the Word object model.
Word.run(function (context) {
// Create a proxy object for the document.
var thisDocument = context.document;
// Queue a command to load the document save state (on the saved property).
context.load(thisDocument,'saved');
// Synchronize the document state by executing the queued commands,// and return a promise to indicate task completion.
return context.sync().then(function () {
if (thisDocument.saved === false) {
// Queue a command to save this document.
thisDocument.save();
// Synchronize the document state by executing the queued commands,// and return a promise to indicate task completion.
return context.sync().then(function () {
console.log('Saved the document');
});
} else {
console.log('The document has not changed since the last save.');
}
});
})
.catch(function (error) {
console.log("Error: " + JSON.stringify(error));
if (error instanceof OfficeExtension.Error) {
console.log("Debug info: " + JSON.stringify(error.debugInfo));
}
});
任何指针都会有很大帮助。
解决方法
这是预期的行为。还没有“另存为”功能,只有“保存”,因此它遵循Word命名文件的默认规则。如果以前从未保存过文件,则将文本的第一行用作文件名。解决方法是,考虑以编程方式将所需的文件名添加为文本的第一行,保存文件,然后删除第一行。