none
Convert .doc to .docx ?

    Question


  • Hi everybody,
    im a new member, please help me !
    i want to convert file doc to docx by programing , but i dont know where to start and how to do ?


    Sunday, October 21, 2007 3:15 AM

Answers

  • You have to open the file in Word, then save to the other file format. There is no way to perform the conversion without automating Word. You require the Documents.Open method, the Document.Save and and the Document.Close methods when automating Word to open, save and close the files.

     

    If you require actual code samples, you have to tell us whethe you're working with VB.NET or C#? Since you're posting in the VSTO forum are you working with a VSTO SE 2005 Add-in?

     

    Monday, October 22, 2007 12:48 PM
  •  Anonymous575419 wrote:

           I would like to now how could i go for converting doc file to docx and docx to html i am using c#. If you could give some link and sample code for this. Thanks I will waiting for your reply.

     

    The only way I know of would be to automate the Word application (although there might be third-party tools on the market that can do this). It would involve opening the document in Word, then saving to the required file type. Here's a bit of sample code in C# that demonstrates how to open a file then save it as HTML. This was written and tested for Word 2007. If you want to save as docx, adjust the file name and WdSaveFormat type. When saving in Word 2007, the Save Format for docx is WdFormatDocument.

     

    Code Block

    object oOpenName = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + \\TestSaveDoc.doc;

    object oSaveName = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\TestSaveDoc.html";

    object oHTMLFormat = Word.WdSaveFormat.wdFormatHTML;

     

    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 oHTMLFormat, 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);

     

     

    Friday, January 04, 2008 5:52 PM

All replies

  • You have to open the file in Word, then save to the other file format. There is no way to perform the conversion without automating Word. You require the Documents.Open method, the Document.Save and and the Document.Close methods when automating Word to open, save and close the files.

     

    If you require actual code samples, you have to tell us whethe you're working with VB.NET or C#? Since you're posting in the VSTO forum are you working with a VSTO SE 2005 Add-in?

     

    Monday, October 22, 2007 12:48 PM
  • hi,

    I also have similar question. I am using word automation with word 2003. We have a requirement to convert the .doc file to xps. I know that docx to  xps can be acheived programatically using word 2007 dll but i dont know how to do this for doc to xps. I have one solution using Microsoft XPS Document Writer which has some limitations, but it would be gr8 if there is any other solution.

     

    Thanks

    Varsha

     

     

     

    Monday, November 19, 2007 12:34 PM
  • Hi Cindy,

           I would like to now how could i go for converting doc file to docx and docx to html i am using c#. If you could give some link and sample code for this. Thanks I will waiting for your reply.

    Thanks,

    Sourabh Jain

    Saturday, December 22, 2007 3:52 PM
  • Hello Cindy,

    I also have got similar task to do (doc to docx), i am open to all, i can either use C# or VB.net to achieve this programatically. I would be more than glad if you could please assist us in this. 


    Tuesday, December 25, 2007 11:20 PM
  • Hi there Cindy,

    You must be busy enjoying the holiday seasons which is quite understandable. However, It will be much appreciated If you could please get back to me on this to your earliest.

    Looking forward to hear from you soon.


    Monday, December 31, 2007 10:26 PM
  • Thanks for reply. I am using c#. I want to convert doc to XPS.

     

    Wednesday, January 02, 2008 5:10 AM
  • Hi Varsha

     

     Varsha555800 wrote:

    I also have similar question. I am using word automation with word 2003. We have a requirement to convert the .doc file to xps. I know that docx to  xps can be acheived programatically using word 2007 dll but i dont know how to do this for doc to xps. I have one solution using Microsoft XPS Document Writer which has some limitations, but it would be gr8 if there is any other solution.

     

    Word 2003 provides no facility for saving files in XPS file format.

     

    Since Word 2003 can save in an XML file format (WordProcessingML), it should theoretically be possible to transform this XML into XPS. However, I have no experience with this, nor any knowledge of a particular tool. I do know that there are companies that provide tools you can use in your programs, such as AltSoft, but I don't know whether they have one for this particular file-type combination.

    Friday, January 04, 2008 5:23 PM
  •  Anonymous575419 wrote:

           I would like to now how could i go for converting doc file to docx and docx to html i am using c#. If you could give some link and sample code for this. Thanks I will waiting for your reply.

     

    The only way I know of would be to automate the Word application (although there might be third-party tools on the market that can do this). It would involve opening the document in Word, then saving to the required file type. Here's a bit of sample code in C# that demonstrates how to open a file then save it as HTML. This was written and tested for Word 2007. If you want to save as docx, adjust the file name and WdSaveFormat type. When saving in Word 2007, the Save Format for docx is WdFormatDocument.

     

    Code Block

    object oOpenName = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + \\TestSaveDoc.doc;

    object oSaveName = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\TestSaveDoc.html";

    object oHTMLFormat = Word.WdSaveFormat.wdFormatHTML;

     

    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 oHTMLFormat, 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);

     

     

    Friday, January 04, 2008 5:52 PM
  •  ibss wrote:

    I also have got similar task to do (doc to docx), i am open to all, i can either use C# or VB.net to achieve this programatically. I would be more than glad if you could please assist us in this. 

     

    Please see my reply to "Anonymous" in this message thread.

    Friday, January 04, 2008 5:53 PM
  •  

    Hi Cindy or any other friends.

     

    I am having issues when opening the .docx file in word 2007 created using the below piece of code.

     

    private static ApplicationClass wdApp = new ApplicationClass();

    static void Main(string[] args)

    {

    object oOpenName = "c:\\Partial Synchronization.doc";

    object oSaveName = "c:\\Partial Synchronization.docx";

    object oDOCXFormat = WdSaveFormat.wdFormatDocument;

    object missing = System.Reflection.Missing.Value;

    try

    {

    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);

    }

    catch (Exception excep)

    {

    string error = excep.Message;

    }

    }

     

    when i tried opening the .docx file in word 2007, it was showing error message saying the file could be corrupted.

     

    My source document is a .doc file created in word 2003? Is this the issue?

     

    Thanks,

    Rathi

    • Proposed as answer by Chaitu Madala Wednesday, July 08, 2009 3:11 PM
    Tuesday, April 29, 2008 2:22 PM
  •  

    Hi Cindy or any other friends.

     

    I am having issues when opening the .docx file in word 2007 created using the below piece of code.

     

    private static ApplicationClass wdApp = new ApplicationClass ();

    static void Main( string [] args)

    {

    object oOpenName = "c:\\Partial Synchronization.doc" ;

    object oSaveName = "c:\\Partial Synchronization.docx" ;

    object oDOCXFormat = WdSaveFormat .wdFormatDocument;

    object missing = System.Reflection. Missing .Value;

    try

    {

    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);

    }

    catch ( Exception excep)

    {

    string error = excep.Message;

    }

    }

     

    when i tried opening the .docx file in word 2007, it was showing error message saying the file could be corrupted.

     

    My source document is a .doc file created in word 2003? Is this the issue?

     

    Thanks,

    Rathi


    Has some one successfully resolved this?
    Wednesday, July 08, 2009 3:13 PM
  •     public class ObjectConstants
        {
            public static Object MissingValue = Missing.Value;
            public static Object True = true;
            public static Object False = true;
        }
    
        class Program
        {
          static void Main(string[] args)
          {
            if (args.Length != 2)
            {
              Console.WriteLine("Usage: DocToDocx 'path-to-doc' 'output-path'");
              return;
            }
            ConvertDocToDocx(args[0], args[1]);
          }
    
    
          public static void ConvertDocToDocx(string docFilePath, string outputDocxFilePath)
          {
            var app = new ApplicationClass();
            app.Visible = false;
            var doc = OpenDocument(app, docFilePath, false);
            SaveDocAsDocx(app, doc, outputDocxFilePath);
            app.Quit(ref ObjectConstants.False, ref ObjectConstants.MissingValue,
     ref ObjectConstants.MissingValue); } public static void SaveDocAsDocx(ApplicationClass app, DocumentClass doc,
    object outputFilePath) { object format = WdSaveFormat.wdFormatXMLDocument; doc.SaveAs(ref outputFilePath, ref format, ref ObjectConstants.False, ref ObjectConstants.MissingValue, ref ObjectConstants.MissingValue, ref ObjectConstants.MissingValue, ref ObjectConstants.MissingValue, ref ObjectConstants.MissingValue, ref ObjectConstants.MissingValue, ref ObjectConstants.MissingValue, ref ObjectConstants.MissingValue, ref ObjectConstants.MissingValue, ref ObjectConstants.MissingValue, ref ObjectConstants.MissingValue, ref ObjectConstants.MissingValue, ref ObjectConstants.MissingValue); } public static DocumentClass OpenDocument(ApplicationClass app, object filePath, bool visible) { var doc = (DocumentClass)app.Documents.Open(ref filePath, ref ObjectConstants.MissingValue, ref ObjectConstants.MissingValue, ref ObjectConstants.MissingValue, ref ObjectConstants.MissingValue, ref ObjectConstants.MissingValue, ref ObjectConstants.MissingValue, ref ObjectConstants.MissingValue, ref ObjectConstants.MissingValue, ref ObjectConstants.MissingValue, ref ObjectConstants.MissingValue, ref ObjectConstants.MissingValue, ref ObjectConstants.MissingValue, ref ObjectConstants.MissingValue, ref ObjectConstants.MissingValue, ref ObjectConstants.MissingValue); if (!visible) { doc.ActiveWindow.Visible = false; } return doc; }

    I use WdSaveFormat.wdFormatXMLDocument value as format parameter to save a document in docx format.
    The above snippet is dependent on Primary Interop Assemblies 2007.

    General advice:
    If you don't know how to code something releted to Office Automation, you can help yourself by using MacroRecorder in Office Word. Just: start recording a macro,  do what you need, stop recording and check a source of the result macro.
    Sunday, August 23, 2009 7:23 PM
  • My example Visual Studio 2010 SP1 C# code to convert .doc to .docx file format (Office 2010):
    1. Download and install Microsoft Visual Studio 2010 Tools for Office
    2. Fire up Visual Studio 2010
    3. Create a new project (Win console app shown here)
    4. In Solution Explorer, add a .NET reference to Microsoft.Office.Interop.Word (file: Microsoft.Office.Interop.Word.dll) - be sure to select Version 14 (Office 2010).
    5. Paste the following code to your Program.cs file, tweak the doc/docx file names, and RUN it:

     

    using System;
    using Microsoft.Office.Interop.Word;
    
    namespace myNamespace
    {
        class Program
        {        
            static void Main(string[] args)
            {
                string docFileName = @"\\serverpath\...\Test.doc";
                string docxFileName = @"\\serverpath\...\Test.docx";
                _Application myWordApp = new Application();
                _Document myDoc = myWordApp.Documents.Open(FileName: docFileName, ReadOnly: true);
                myDoc.SaveAs2(FileName: docxFileName, FileFormat: WdSaveFormat.wdFormatDocumentDefault, CompatibilityMode: WdCompatibilityMode.wdWord2010);
    
    	   myDoc.Close();
                myWordApp.Quit();            
            }//main
        }//class
    }//namespace
    
    Of course there are several options for the calls to .Open() and .SaveAs2() but the options shown should work in most cases.  It works nicely for me.  I hope it works nicely for you too!





    Friday, September 23, 2011 8:24 PM
  •  Have a try to use Spire.Doc, Convert Word 2003 doc to Word 2007 docx supported without Office installed.

    using System;
    using Spire.Doc;
    using Spire.Doc.Documents;
    namespace DocxToDoc
    {
    class Program
    {
    static void Main(string[] args)
    {
    //Create a new document
    Document document = new Document();
    //Load word 2003 file from disk.
    document.LoadFromFile("WordFile.doc");
    //Save as word 2007 file.
    document.SaveToFile("Sample.docx", FileFormat.Docx);
    }
    }
    }
    Tuesday, September 27, 2011 7:03 AM
  • I think... you need something like this:

    			SautinSoft.UseOffice u = new SautinSoft.UseOffice();
    
    			//Path to any local file
    			string inputFilePath = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(),@"..\..\..\..\..\..\Testing files\DOC.doc"));
    			//Path to output resulted file
    			string outputFilePath = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(),@"..\..\..\..\..\..\Testing files\Result.docx"));
    
    			//Prepare UseOffice .Net, loads MS Word in memory
    			int ret = u.InitWord();			
    
    			//Return values:
    			//0 - Loading successfully
    			//1 - Can't load MS Word® library in memory 
    
    			if (ret==1)
    				return;
    			
    			//Converting
    			ret = u.ConvertFile(inputFilePath, outputFilePath, SautinSoft.UseOffice.eDirection.DOC_to_DOCX);
    
    			//Release MS Word from memory
    			u.CloseWord();
    
    			//0 - Converting successfully
    			//1 - Can't open input file. Check that you are using full local path to input file, URL and relative path are not supported
    			//2 - Can't create output file. Please check that you have permissions to write by this path or probably this path already used by another application
    			//3 - Converting failed, please contact with our Support Team
    			//4 - MS Office isn't installed. The component requires that any of these versions of MS Office should be installed: 2000, XP, 2003, 2007 or 2010
    			if(ret==0 )
    			{
    				//Show produced file
    				System.Diagnostics.Process.Start(outputFilePath);
    			}		
    
    

    Read more: sautinsoft.com


    SautinSoft - .Net components to convert between PDF, HTML, RTF, DOC, XHTML, EXCEL, IMAGES, HTML Beauty etc. http://www.sautinsoft.com
    Tuesday, October 04, 2011 10:58 AM
  • Hi Nguyen Nam,

    I have got your problem, as earlier I also had huge bunch of 2000-2003 Word Files, then I opt for a third party tool. SysTools Office Upgrade tool converted thousand plus Word files in batch to Docx format.

    I m a user of this tool I will suggest you to take this tool, so you may be able to perform bulk conversion from DOC to DOCX Files, XLS to XLSX File, PPT to PPTX Files.

    You may try: http://www.systoolsgroup.com/office-upgrade.html

    Thanks
    Adom Jada

    Thursday, March 29, 2012 4:48 AM
  • Hi, you can also try this .NET Word dll.

    It is a simple and straightforward way to convert DOC to a DOCX file in C#.

    DocumentModel.Load("Word 2003 file.doc").Save("Word Open XML file.docx");

    Btw, Anonymous8637 you can also convert DOCX to HTML in C# in the same manner:

    DocumentModel.Load("Document.docx").Save("Document.html");

    Tuesday, September 17, 2013 8:21 AM