首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >wdFormatDocument97格式的Word文档SaveAs2

wdFormatDocument97格式的Word文档SaveAs2
EN

Stack Overflow用户
提问于 2016-07-21 23:32:49
回答 1查看 4.9K关注 0票数 1

我使用Microsoft Interop Word版本15.0.0.0来创建一个新的Word文档,在其中插入一些文本,然后保存它。

当我使用以下命令保存它时:

代码语言:javascript
复制
document.SaveAs2(wordFilePath);

文档以DOCX格式保存。

但是当我使用以下命令保存它时:

代码语言:javascript
复制
document.SaveAs2(wordFilePath, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument97);

文档看似保存为Word-97 DOC (Windows资源管理器显示为Word-97 DOC图标和类型),但它实际上在内部保存为DOCX (我可以从两个方面看到这一点:它与相应的DOCX具有相同的大小,当我使用Word-2016打开它并选择SaveAs时,默认的保存格式是DOCX!)。

如何以real document-97格式保存文档?

下面是用于创建新Word文档的函数,该文档的类型取决于给定文件路径的扩展名(DOC与DOCX):

代码语言:javascript
复制
public static void TextToMsWordDocument(string body, string wordFilePath)
{
    Microsoft.Office.Interop.Word.Application winword = new Microsoft.Office.Interop.Word.Application();
    winword.Visible = false;
    object missing = System.Reflection.Missing.Value;
    Microsoft.Office.Interop.Word.Document document = winword.Documents.Add(ref missing, ref missing, ref missing, ref missing);
    if (body != null)
    {
        document.Content.SetRange(0, 0);
        document.Content.Text = (body + System.Environment.NewLine);
    }
    if (System.IO.Path.GetExtension(wordFilePath).ToLower() == "doc")
        document.SaveAs2(wordFilePath, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument97);
    else // Assuming a "docx" extension:
        document.SaveAs2(wordFilePath);
    document.Close(ref missing, ref missing, ref missing);
    document = null;
    winword.Quit(ref missing, ref missing, ref missing);
    winword = null;
}

下面是用于调用此函数的代码:

代码语言:javascript
复制
TextToMsWordDocument("abcdefghijklmnopqrstuvwxyz", "text.doc");
TextToMsWordDocument("abcdefghijklmnopqrstuvwxyz", "text.docx");
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-07-27 18:14:11

这是一个相当愚蠢的error...compare‘==’.doc‘’而不是‘==’doc‘...

我没有注意到这一点,因为当SaveAs2收到一个扩展名为".doc“但没有WdSaveFormat的文件路径时,它-奇怪的是-创建了一个Word文档文件,其中存在我在这里解释的问题……

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38508436

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档