none
Converting Word 97 to Word 2010 file format RRS feed

  • Question

  • How can I programmatically convert word 97 (.doc) file into Word 201o (.docx) using C#?

    Adeel


    Wednesday, July 25, 2012 8:07 AM

Answers

  • Hi Adeel

    Look at the Document.SaveAs method.

    Note that in this case you will need to use the Format command. Read the WdSaveFormat enumeration carefully. You should choose wdFormatXMLDocument if you want to save as *.docx.


    Cindy Meister, VSTO/Word MVP

    • Marked as answer by maverick786us Thursday, July 26, 2012 5:52 AM
    Wednesday, July 25, 2012 9:35 AM
    Moderator
  • The SaveAs method that Cindy talks about is a method that belongs to all Word Document objects.

    A document object is returned by the following:

    oWord.Documents.Open(File);

    It's just a case of saving it....so:

    MSWord.Document myDocument = oWord.Documents.Open(File);

    Then you can apply the SaveAs method.

    myDocument.SaveAs(//arguments);
    • Marked as answer by maverick786us Thursday, July 26, 2012 5:52 AM
    Wednesday, July 25, 2012 10:26 AM
  • If you want to use Word Interop, the best you can do is open a document using a hidden Word application (wordApp.Visible = false).
    • Marked as answer by maverick786us Thursday, July 26, 2012 5:52 AM
    Wednesday, July 25, 2012 12:33 PM
  • When you use 'SaveAs', doesn't that document then become the active one? So, if you open 'C:\Test01.doc', and then save it as 'C:\Test02.docx', you're then safe to delete Test01.doc, because the document has become Test02.docx.

    I could be wrong.

    • Marked as answer by maverick786us Thursday, July 26, 2012 10:18 AM
    Thursday, July 26, 2012 9:09 AM
  • Hi Adeel

    You must open a document in order to convert it.

    Or Word Automation Services (part of SharePoint) has to be available, server-side.

    Or you have to purchase a third-party tool that can convert the old *.doc file format in *.docx, without the help of the Word Application.


    Cindy Meister, VSTO/Word MVP

    • Marked as answer by maverick786us Thursday, July 26, 2012 5:52 AM
    Wednesday, July 25, 2012 11:20 AM
    Moderator

All replies

  • IN what kind of environment? Meaning...

    1. Is this a server environment or would it be on a local machine?

    2. Would you have a recent version of SharePoint available? (Thing about Word Automation Services, which would be the best approach.)


    Cindy Meister, VSTO/Word MVP

    Wednesday, July 25, 2012 9:05 AM
    Moderator
  • IN what kind of environment? Meaning...

    1. Is this a server environment or would it be on a local machine?

    2. Would you have a recent version of SharePoint available? (Thing about Word Automation Services, which would be the best approach.)


    Cindy Meister, VSTO/Word MVP


    Local Machine in which the user has MS Office 2010 installed

    Adeel

    Wednesday, July 25, 2012 9:06 AM
  • Hi Adeel

    Look at the Document.SaveAs method.

    Note that in this case you will need to use the Format command. Read the WdSaveFormat enumeration carefully. You should choose wdFormatXMLDocument if you want to save as *.docx.


    Cindy Meister, VSTO/Word MVP

    • Marked as answer by maverick786us Thursday, July 26, 2012 5:52 AM
    Wednesday, July 25, 2012 9:35 AM
    Moderator
  • Hi Adeel

    Look at the Document.SaveAs method.

    Note that in this case you will need to use the Format command. Read the WdSaveFormat enumeration carefully. You should choose wdFormatXMLDocument if you want to save as *.docx.


    Cindy Meister, VSTO/Word MVP

    According to this artical. _DocumentSaveAs belongs to Microsoft.Office.Interop.Word namespace. Now How can I access this method. I declared my objects like this.

    using MSWord = Microsoft.Office.Interop.Word;
    private MSWord.Application oWord;
    oWord = (MSWord.Application)applicationObject;

    This oWord holds the current context. Now I have the path of the .doc file so how can I access _DocumentSaveAs from oWord to convert it?

    Adeel

    Wednesday, July 25, 2012 10:00 AM
  • Secondly DocumentSave as is used for current document. In my case I will provide the path of the .doc file that has to be converted into .docx format.

    Adeel

    Wednesday, July 25, 2012 10:10 AM
  • The SaveAs method that Cindy talks about is a method that belongs to all Word Document objects.

    A document object is returned by the following:

    oWord.Documents.Open(File);

    It's just a case of saving it....so:

    MSWord.Document myDocument = oWord.Documents.Open(File);

    Then you can apply the SaveAs method.

    myDocument.SaveAs(//arguments);
    • Marked as answer by maverick786us Thursday, July 26, 2012 5:52 AM
    Wednesday, July 25, 2012 10:26 AM
  • The SaveAs method that Cindy talks about is a method that belongs to all Word Document objects.

    A document object is returned by the following:

    oWord.Documents.Open(File);

    It's just a case of saving it....so:

    MSWord.Document myDocument = oWord.Documents.Open(File);

    Then you can apply the SaveAs method.

    myDocument.SaveAs(//arguments);

    Thank you Joseph. This thing looks perfectly fine. But can this be done before opening the document. Because when I have to Save it into .docx before opening it.

    Thanks in Advance


    Adeel

    Wednesday, July 25, 2012 10:53 AM
  • Hi Adeel

    You must open a document in order to convert it.

    Or Word Automation Services (part of SharePoint) has to be available, server-side.

    Or you have to purchase a third-party tool that can convert the old *.doc file format in *.docx, without the help of the Word Application.


    Cindy Meister, VSTO/Word MVP

    • Marked as answer by maverick786us Thursday, July 26, 2012 5:52 AM
    Wednesday, July 25, 2012 11:20 AM
    Moderator
  • Hi Adeel

    You must open a document in order to convert it.

    Or Word Automation Services (part of SharePoint) has to be available, server-side.

    Or you have to purchase a third-party tool that can convert the old *.doc file format in *.docx, without the help of the Word Application.


    Cindy Meister, VSTO/Word MVP


    OK Is there a way it silently opens the file (without letting the user to know) and then converts into .docx and then open that .docx file? Because I have first converted this .odt file into .doc and than that .doc into .docx. So I don't want the user to see the first conversion

    Adeel

    Wednesday, July 25, 2012 11:45 AM
  • If you want to use Word Interop, the best you can do is open a document using a hidden Word application (wordApp.Visible = false).
    • Marked as answer by maverick786us Thursday, July 26, 2012 5:52 AM
    Wednesday, July 25, 2012 12:33 PM
  • If you want to use Word Interop, the best you can do is open a document using a hidden Word application (wordApp.Visible = false).

    And 2nd thing is, my plugin converts .odt into .docx. Now open office SDKs cannot convert an .odt file into .docx. However it can convert it into Word 97 format (.doc). So I have to first convert it into .doc and then .docx. So there is a  small problem. This overall process creates 2 files. 1. The converted .doc file and other the .docx file. So what is the best way to avoid this. Do I have to use the cheap way of deleting the .doc file once .docx file has been created or is there some other way?

    Adeel

    Thursday, July 26, 2012 4:19 AM
  • Anyways I used this whole code to get my job done.

    1) It converts .odt file into .doc file
    2) Opens that .doc file
    3) Then it converts .doc file into .docx file
    4) Closes that .doc file
    5) Deletes that .doc file
    6) Opens the finally created .docx file

    Here is the code

    object fileFormat = MSWord.WdSaveFormat.wdFormatDocumentDefault;
    oWord.ActiveDocument.Close(); // close the currently loaded .odp file
    saveDocument(xComponent, File, PathConverter(File)); // Converting from .odp to .doc
    
    Extension = Path.GetExtension(File);
    oWord.Visible = false;
    Document = oWord.Documents.Open(File); // open the newly created .doc file
    File = File.Replace(".doc", ".docx");
    Document.SaveAs(File, fileFormat); // convert the .doc file into .docx
    oWord.ActiveDocument.Close(); // close the .doc file 
    System.IO.File.Delete(oldFile); // delet the .doc file
    oWord.Visible = true;
    oWord.Documents.Open(File); // reload the .docx file

    This whole thing seems to work fine, there is just one small problem. It restarts the whole word. How can I overcome this.

    Adeel


    Thursday, July 26, 2012 6:07 AM
  • I have to confess I don't have experience with .ODTfiles.

    However, I'm happy to have a punt! From your example it looks like you're opening the .ODT file with Word. I would have thought you could then save it directly to .DOCX. I don't think you need to close the document, and re-open it.


    • Edited by JosephFox Thursday, July 26, 2012 6:56 AM
    Thursday, July 26, 2012 6:55 AM
  • I have to confess I don't have experience with .ODTfiles.

    However, I'm happy to have a punt! From your example it looks like you're opening the .ODT file with Word. I would have thought you could then save it directly to .DOCX. I don't think you need to close the document, and re-open it.



    It makes a copy of the document and then convert to (Just like the case of Save Aa). So the original document has to be closed and then the converted document has to be opened

    Adeel

    Thursday, July 26, 2012 8:50 AM
  • When you use 'SaveAs', doesn't that document then become the active one? So, if you open 'C:\Test01.doc', and then save it as 'C:\Test02.docx', you're then safe to delete Test01.doc, because the document has become Test02.docx.

    I could be wrong.

    • Marked as answer by maverick786us Thursday, July 26, 2012 10:18 AM
    Thursday, July 26, 2012 9:09 AM
  • Got the point. It works. Thanks


    Adeel

    Thursday, July 26, 2012 11:00 AM