开发者

Automation: how to automate transforming .doc to .docx?

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 开发者_如何学Python.docx

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

How would you automate this?


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"


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

ofc

and the magic happens.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜