Converting .doc to docx using Microsoft.Office.Interop.Word v12

A

Andrew

Hi,

I want to programatically convert a .doc file to .docx
I am working in c# and as indicated in the subject line I am using the
Office.Interop libraries for Office 2007 (Microsoft.Office.Interop.Word.dll v
12.0.6211.1000).

I am working with code similar to the following;

object oOpenName = c:\\Temp\\DocTestOpen.doc;

object oSaveName = object oOpenName = c:\\Temp\\DocxTestSave.docx;

object oDocXFormat = Word.WdSaveFormat.wdFormatDocument;


Word.Document doc = wdApp.Documents.Open(ref oOpenName, ref missing, ref
missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref
missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref
missing, ref missing);

doc.SaveAs(ref oSaveName, ref oDocXFormat , ref missing, ref missing, ref
missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref
missing, ref missing, ref missing, ref missing, ref missing, ref missing);

I created the source .doc file (DocTestOpen.doc) using Office 2007 saving
the file as a .doc so it is a Word 97 formatted file.

When I use the WdSaveFormat.wdFormatDocument ie
object oDocXFormat = WdSaveFormat.wdFormatDocument;
I am told that the document is corrupt. Renaming the file to a .doc
extension reveals that the file was not converted and is still a .doc (office
97) format.

I also tried
object oDOCXFormat = WdSaveFormat.wdFormatXMLDocument;

Using wdFormatXMLDocument the file can be opened however Word 2007 indicates
that it is in compatibility mode and when I go 'Save As' even though the file
type list is set to .docx when I click save it prompts me to convert the
document. ie

'You are about to sae your document to one of the new file formats. .... etc
etc"

Searching the net has not revealed an obvious solution. I don't know if
this means no one else is doing this or I have missed the obvious.

Thanks,

Andrew
 
N

Neil Cumfer

Before you save it in the Word 2007 format (wdFormatXMLDocument), you
must first
convert it to that format if you don't want it to be saved in
compatibility mode

I don't know what the c# code would be, but my guess is
doc.Convert

In Word 2007 the manual steps would be, if you have a document open in
compatibility mode, click on the Office Button and choose Convert before
saving it.

Andrew said:
Hi,

I want to programatically convert a .doc file to .docx
I am working in c# and as indicated in the subject line I am using the
Office.Interop libraries for Office 2007
(Microsoft.Office.Interop.Word.dll v
 
A

Andrew

Hi Neil,

That's great you may not be familiar with c# but your suggestion has
resolved my problem. Thankyou.

A couple of comments.

The online msdn help is obviously still Office v11 not 12 since this method
isn't documented. Through various searches the only Office Interop help that
I could find was here
http://msdn.microsoft.com/fr-fr/library/microsoft.office.interop.word._document_members(VS.80).aspx

Is there another location where I can find up todate help?

Any sample code I found suggested that we the SaveAs method but presumably
that is a hangover from Office 11/2003.

When manually using the save as menu option the same result was achieved as
using the convert menu option so I had expected similar behaviour using the
Interop.

Thanks,

Andrew
 
A

Also Andrew

Hey Andrew,

I am trying to do a similar thing. I understand the discussion, can you
post the code that worked for you?

Thanks,
Also Andrew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top