将MS Word另存为docx而不是doc c#

将MS Word另存为docx而不是doc c#,c#,C#,当我试图在winform C#中保存一个具有docx扩展名的文件并打开该文件时,出现异常: “word无法打开文件,因为文件格式与文件扩展名不匹配” 以下是我保存文件的方式: object oMissing = Missing.Value; Word.Application oWord = new Word.Application(); Word.Document oWordDoc = new Word.Document(); oWord.Visible = false; oWordDoc =

当我试图在winform C#中保存一个具有docx扩展名的文件并打开该文件时,出现异常:
“word无法打开文件,因为文件格式与文件扩展名不匹配”

以下是我保存文件的方式:

object oMissing = Missing.Value;
Word.Application oWord = new Word.Application();
Word.Document oWordDoc = new Word.Document();
oWord.Visible = false;
oWordDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);  

Object oSaveAsFile = (Object)@"C:\test\FINISHED_XML_Template.docx";            
        oWordDoc.SaveAs(ref oSaveAsFile, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing);

        oWordDoc.Close(false, ref oMissing, ref oMissing);
        oWord.Quit(ref oMissing, ref oMissing, ref oMissing);

此页面建议更改为使用
Word.Application.SaveAs2()
方法


请参见此链接中的答案:

Daves解决方案如下所示,但我已将Daves代码从 CompatibilityMode:=WdCompatibilityMode.wdWord2010到CompatibilityMode:=WdCompatibilityMode.wdWord2013更为最新,并为您提供真正的docx(无兼容模式)


您使用的Office COM互操作库版本是否支持
docx
文件?我想是的,我使用的是Microsoft Office 12.0对象库在
SaveAs
方法中,您可以尝试将
Word.WdSaveFormat.wdFormatXMLDocument
指定为第二个参数值吗?
public void ConvertDocToDocx(string path)
{
    Application word = new Application();

    if (path.ToLower().EndsWith(".doc"))
    {
        var sourceFile = new FileInfo(path);
        var document = word.Documents.Open(sourceFile.FullName);

        string newFileName = sourceFile.FullName.Replace(".doc", ".docx");
        document.SaveAs2(newFileName,WdSaveFormat.wdFormatXMLDocument, 
                         CompatibilityMode: WdCompatibilityMode.wdWord2013);

        word.ActiveDocument.Close();
        word.Quit();

        File.Delete(path);
    }
}