In this blog I am trying to explain the concept of how to convert a document to rich text file format using C#. Here I am going to make a console application which accept a word file and convert in to a rtf file format.

Getting start: -

1.       Create a console application using visual studio with appropriate name.

2.       Create a method with DocToRtf() name below I provide you its code.

private static void DocToRtf()
{
     //Creating the instance of Word Application
     Microsoft.Office.Interop.Word.Application newApp = new Microsoft.Office.Interop.Word.Application();      // Give the Source file name & Target file names
     object Source = "Source File Name";
     // Give the Target file names      object Target = "Target File Name";
     // 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 = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML;
    //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);

}

3.       Call this method in Main() method.

4.       Debug the application and go to the target file path and see the result.

  Modified On Sep-18-2014 01:24:19 PM

Leave Comment