C# Word Interop Saving docx with Word 2003 + Compa

2019-08-29 11:38发布

问题:

On my dev machine, I have office 2007 installed on Win7 x64.

The target machines are Office 2003 + Compatibility Pack (to read/write docx, etc.) running on WinXP x64.

My program does, among other things, open/save as commands on doc files, converting them to docx.

It runs wonderfully on my machine, but sadly gives zero-sized files on the target machines. No exceptions are thrown, it looks like it's working fine except when I look at the output file, it's zero-length.

Any ideas why this is happening, and how to get around it?

My last-resort will be to write an ugly Word/PowerPoint macro which opens a document and saves it. I would prefer everything to happen automatically from my program without opening a Word/PowerPoint window.

回答1:

There is no need to automate Word, which is rather slow and brittle due to pop-up messages You might want to have a look at Microsoft's Office File Converter (ofc.exe), which however has an unnecessarily complicated user interface.

The simplest and fastest way would be to 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"

You can run this command from C# using the Process.Start method.



回答2:

Are you sure that the SAVE or SAVEAS you're using is specifying the right options to force the file to save as a DOCX? Under 2007, that's built in, but under 2003, I believe you have to specify the save format a different way, explicitly telling it to use the appropriate filter from the compatibility pack.