如何在使用 word 加载项时使用 office js API 或任何其他方式以文件名保存 word 文档?

How to save word document with filename using office js API or any other way while working with word add-in?

我浏览了此文档并遵循了此处提到的示例,但无法使用文件名保存文档。 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 的默认文件命名规则。如果该文件以前从未保存过,则第一行文本用作文件名。作为解决方法,考虑以编程方式将所需文件名添加为文本的第一行,保存文件,然后删除第一行。