格式为 wdFormatDocument97 的 Word 文档 SaveAs2
Word Document SaveAs2 in Format wdFormatDocument97
我正在使用 Microsoft Interop Word 版本 15.0.0.0 创建一个新的 Word 文档,向其中插入一些文本,然后保存。
当我使用以下命令保存它时:
document.SaveAs2(wordFilePath);
文档以 DOCX 格式保存。
但是当我使用以下命令保存它时:
document.SaveAs2(wordFilePath, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument97);
文档看似保存为 Word-97 DOC(Windows 资源管理器以 Word-97 DOC 图标和类型显示它),但它实际上在内部保存为 DOCX(我可以通过两种方式查看: 和对应的DOCX一样大,用Word-2016打开,select另存为,默认保存格式是DOCX!).
如何将文档保存为真正的 document-97 格式?
这是用于创建新 Word 文档的函数,其类型取决于给定文件路径的扩展名(DOC 与 DOCX):
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;
}
下面是调用此函数的代码:
TextToMsWordDocument("abcdefghijklmnopqrstuvwxyz", "text.doc");
TextToMsWordDocument("abcdefghijklmnopqrstuvwxyz", "text.docx");
这是一个相当愚蠢的错误...比较‘==“.doc”’而不是‘=="doc"...
我没有注意到它,因为当 SaveAs2 收到扩展名为“.doc”但没有 WdSaveFormat 的文件路径时,它 - 很奇怪 - 创建了一个 Word 文档文件,其中存在我在此处解释的问题。 ..
我正在使用 Microsoft Interop Word 版本 15.0.0.0 创建一个新的 Word 文档,向其中插入一些文本,然后保存。
当我使用以下命令保存它时:
document.SaveAs2(wordFilePath);
文档以 DOCX 格式保存。
但是当我使用以下命令保存它时:
document.SaveAs2(wordFilePath, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument97);
文档看似保存为 Word-97 DOC(Windows 资源管理器以 Word-97 DOC 图标和类型显示它),但它实际上在内部保存为 DOCX(我可以通过两种方式查看: 和对应的DOCX一样大,用Word-2016打开,select另存为,默认保存格式是DOCX!).
如何将文档保存为真正的 document-97 格式?
这是用于创建新 Word 文档的函数,其类型取决于给定文件路径的扩展名(DOC 与 DOCX):
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;
}
下面是调用此函数的代码:
TextToMsWordDocument("abcdefghijklmnopqrstuvwxyz", "text.doc");
TextToMsWordDocument("abcdefghijklmnopqrstuvwxyz", "text.docx");
这是一个相当愚蠢的错误...比较‘==“.doc”’而不是‘=="doc"...
我没有注意到它,因为当 SaveAs2 收到扩展名为“.doc”但没有 WdSaveFormat 的文件路径时,它 - 很奇怪 - 创建了一个 Word 文档文件,其中存在我在此处解释的问题。 ..