tags:

views:

167

answers:

5

I have a bunch of .doc files in a folder which I need to convert to .docx.

To manually convert the .doc to .docx is pretty simple:

  1. Open .doc in Word 2007
  2. Click on Save As...
  3. Save it as .docx

However, doing this for hundreds of files definitely ain't fun. =p

How would you automate this?

A: 

WD2000: How to Use (OLE) Automation with Word. Is for Word 2000, but the model still applies.

Remus Rusanu
+2  A: 

Automate Word.

If you are using .NET, add Microsoft.Office.Interop.Word (make sure it is version 12 - equivalent to Word 2007 so you can achieve the above) reference assembly to your project and use it it automate word app to do exactly what you want to do above. The pseudocode

  • Create the application object
  • Use the application object to open a document (by supplying it the file name)
  • Use the application object to perform SaveAs by supplying to it the format and output filename
  • Close the current document
  • Loop through the above till you finish with all documents
  • Housekeeping code to release the Word or Doc objects

You can find plenty of example on google, just search for Word Automation in C# or something along that line.

Fadrian Sudaman
+4  A: 

The easiest way is to use the command-line Office File Converter. Just run

ofc

and the magic happens.

John Feminella
+1 Great, totally forgot about this. It will be much simpler.
Fadrian Sudaman
A: 

There is no need to automate Word, which is rather slow and brittle due to pop-up messages, or to use Microsoft's Office File Converter (ofc.exe), which has an unnecessarily complicated user interface.

The simplest and fastest way would be to install either Office 2007 or download and install the Compatibility Pack from Microsoft (if not already done). Then you can convert from .doc to .docx easily using the following command:

"C:\Program Files\Microsoft Office\Office12\wordconv.exe" -oice -nme <input file> <output file>

where <input file> and <output file> need to be fully qualified path names.

The command can be easily applied to multiple documents using for:

for %F in (*.doc) do "C:\Program Files\Microsoft Office\Office12\wordconv.exe" -oice -nme "%F" "%Fx"
0xA3