Share via


How to convert doc file to docx file using C#

Question

Sunday, November 30, 2014 12:18 PM

Hi All,

I faced an issue when i using the following code to convert doc to docx file. I didn't how to correct it? Could you please help me with this issue?

using MSWord = Microsoft.Office.Interop.Word;

namespace DocToDocx
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Convert doc to docx:");
            MSWord.Application wordApp = new MSWord.Application() { Visible = false };
            MSWord.Document doc = wordApp.Documents.Open(@"D:\P-Materials\Development\VSTO\Word2003Format.doc");
            doc.SaveAs2("D:/Word2003Format.docx", MSWord.WdSaveFormat.wdFormatDocument);
            Console.WriteLine("OK!");
            doc.Close();
            wordApp.Quit();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(doc);
            Console.ReadKey();

        }
    }
}

Thanks a lot,

All replies (1)

Sunday, November 30, 2014 12:28 PM âś…Answered

Hello Ricky,

> I faced an issue

Could you please be more specific? What issue are you talking about?

doc.SaveAs2("D:/Word2003Format.docx", MSWord.WdSaveFormat.wdFormatDocument);

The wdFormatDocument corresponds to Microsoft Office Word 97 - 2003 binary file format. You need to use the wdFormatDocumentDefault instead - Word default document file format. For Word 2010, this is the DOCX format.

So, the code should look like the following one:

doc.SaveAs2("D:/Word2003Format.docx", MSWord.WdSaveFormat.wdFormatDocumentDefault);