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

问题描述

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

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...