C# – Convert DOC to DOCX


Hello everyone. Today I tried to figure out how I can convert doc files to docx files by using C#. Well, and I came up with the below example.

The prerequisites for this solution are:

  • Visual Studio
  • Microsoft Office installed on the machine where you want to run this application

Now I will explain the necessary steps.

The first thing you need to do is to add a reference by clicking on Add Reference…

visual_studio_doc_to_docx_add_reference

Now search for Microsoft Word 14.0 Object Library (The version number 14 represents Microsoft Office 2010. Version number 15.0 represents Microsoft Office 2013) and click OK.

visual_studio_doc_to_docx_microsoft_word_object_library

Important: Make sure you have a version of Microsoft Office installed on your Development machine. Otherwise you will not find the reference you need.

Now for the code. Here you go:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DOCToDOCX
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void browse_Click(object sender, EventArgs e)
{
if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
pathTextBox.Text = openFileDialog1.FileName;
}
}

private void convert_Click(object sender, EventArgs e)
{
Function f = new Function();

f.ConvertDOCToDOCX(pathTextBox.Text);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Word;
using System.IO;

namespace DOCToDOCX
{
class Function
{
public void ConvertDOCToDOCX(string path)
{
Application word = new Application();

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

string newFileName = sourceFile.FullName.Replace(".doc", ".docx");
doc.SaveAs2(FileName: newFileName, FileFormat: WdSaveFormat.wdFormatXMLDocument, CompatibilityMode: Microsoft.Office.Interop.Word.WdCompatibilityMode.wdWord2010);

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

Important: If you remove the following line of code from the above code your documents will stay in Compatibility Mode after they were converted to docx files.

CompatibilityMode: Microsoft.Office.Interop.Word.WdCompatibilityMode.wdWord2010

That´s it. Pretty simple, right? Hopefully my post was interesting for you. And as always you can download my code from here.

Sources:

http://justgeeks.blogspot.co.at/2011/09/convert-batch-of-doc-files-to-docx.html

http://msdn.microsoft.com/en-us/library/15s06t57.aspx

http://row5is.wordpress.com/2012/07/06/convert-doc-to-docx-using-powershell/

http://msdn.microsoft.com/en-us/library/microsoft.office.tools.word.document.saveas2.aspx

Advertisements
Tagged

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: