Click here to Skip to main content
15,881,089 members
Articles / Programming Languages / C#
Article

How to convert DOC into other formats using C#

Rate me:
Please Sign up or sign in to vote.
3.24/5 (30 votes)
22 Oct 20031 min read 224.9K   50   41
This article explains how to convert DOC into other formats using C#.

Introduction

This article will only show you how to use the COM object library in C#, so you can easily convert a DOC file into different formats that are supported by Microsoft Word. This could be accomplished by using the Microsoft Word 10.0 Object library. You can add this library by using the menu bar of .NET IDE, that is ProjectAdd Reference…- COM, here you can add the reference to the Microsoft Word 10.0 Object library for your code & it is necessary for the conversion. By adding this library as a reference in your project, you can now use the Word namespace which will allow you to do all the stuff. So it is necessary to add this reference into your project.

Conversion code

The following code shows how to convert DOC to RTF. You can convert to any format which is supported by MS Word by only changing the type of Target file.

C#
using System; 

namespace DocConvert 

{ 
      class DoctoRtf 
      { 
            static void Main() 
            { 

                //Creating the instance of Word Application
                Word.Application newApp = new Word.Application(); 

                // specifying the Source & Target file names
                object Source="c:\\abc\\Source.doc";
                object Target="c:\\abc\\Target.rtf";

                // Use for the parameter whose type are not known or  
                // say Missing
                object Unknown =Type.Missing;

                // Source document open here
                // Additional Parameters are not known so that are  
                // set as a missing type
                newApp.Documents.Open(ref Source,ref Unknown, 
                     ref Unknown,ref Unknown,ref Unknown, 
                     ref Unknown,ref Unknown,ref Unknown, 
                     ref Unknown,ref Unknown,ref Unknown, 
                     ref Unknown,ref Unknown,ref Unknown,ref Unknown);

        // Specifying the format in which you want the output file 
                object format = Word.WdSaveFormat.wdFormatRTF;

        //Changing the format of the document
                newApp.ActiveDocument.SaveAs(ref Target,ref format, 
                        ref Unknown,ref Unknown,ref Unknown, 
                        ref Unknown,ref Unknown,ref Unknown, 
                        ref Unknown,ref Unknown,ref Unknown, 
                        ref Unknown,ref Unknown,ref Unknown, 
                        ref Unknown,ref Unknown);                    

                // for closing the application
                newApp.Quit(ref Unknown,ref Unknown,ref Unknown);

            } 
      } 
}

What the code does

If you want to convert the particular file, then you have to first open that file. You can do this by using the Documents class in the Word namespace & then call the Open method which has a number of parameters, out of which source filename is important. Others you can say as missing. Then use the ActiveDocument’s SaveAs to save the file in a target format and lastly close all the documents by using Quit.

Reminders

Make sure that you include the reference to Microsoft Word 10.0 object library in your code; otherwise the compiler will flag error for the Word namespace.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Engineer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: browser hangs while converting Pin
Anonymous3-Jan-05 19:13
Anonymous3-Jan-05 19:13 
GeneralRe: browser hangs while converting Pin
Mike Reagan28-Jan-05 8:13
Mike Reagan28-Jan-05 8:13 
Generalif we test this code t o convert doc2html..... Pin
hamden16-Jun-04 0:38
hamden16-Jun-04 0:38 
GeneralThe same in C++ Pin
Member 10802319-May-04 22:46
Member 10802319-May-04 22:46 
QuestionDoes the User need Word for this? Pin
tonictickle5-Mar-04 0:44
tonictickle5-Mar-04 0:44 
AnswerRe: Does the User need Word for this? Pin
Michael P Butler5-Mar-04 1:09
Michael P Butler5-Mar-04 1:09 
GeneralCode not working Pin
Amol Jaysingpure12-Nov-03 1:48
Amol Jaysingpure12-Nov-03 1:48 
GeneralRe: Code not working Pin
Prashant Agrawal12-Nov-03 3:19
Prashant Agrawal12-Nov-03 3:19 
GeneralRe: Code not working Pin
Amol Jaysingpure12-Nov-03 17:50
Amol Jaysingpure12-Nov-03 17:50 
GeneralRe: Smalll Error Pin
Prashant Agrawal13-Nov-03 16:51
Prashant Agrawal13-Nov-03 16:51 
GeneralRe: Code not working Pin
John Liu16-Nov-03 17:25
John Liu16-Nov-03 17:25 
GeneralOne small change... Pin
je_gonzalez22-Oct-03 5:29
je_gonzalez22-Oct-03 5:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.