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

Converting RTF to TXT format

Rate me:
Please Sign up or sign in to vote.
4.82/5 (20 votes)
13 Aug 2005CPOL2 min read 256.2K   1.8K   46   53
Using Word Automation for converting files to XML, RTF, TXT, HTML, etc. formats.

Image 1

Introduction

Often there is a need to convert a file from one format to another. Most of the tools out there seem to cost some money. Not a lot of money, but they aren’t free. Anyway, several file types that you may want to convert to can be done through Microsoft Word. Here is an example of using C# to access Word Automation object to read in a file type and then write out a different file type.

Background

I had a need to convert an RFT format to TXT. As I was looking for solutions, I had a hard time finding one that was free. So I decided to use Word Automation to accomplish my goals. Note that in this example I am using Office 2003. If you do not have Office 2003 this will still probably work except for the XML output since that is new to Word 2003. The sample program I have included in the download with the source code allows you to set an input file. This is loaded into the Word object. Then there is a ComboBox that contains a list of formats that you can convert to.

The code

To do Word Automation you need to add a reference to your project to the Word DLL.

Image 2

Add a reference to your project. Click on the COM tab. Down towards the bottom you will find a Microsoft Word 11.0 Object Library. Select and add to references. Now you will be able to access the Word functionality in code.

C#
private void ConvertFile()
{
  String inFileName = txtBoxInputFile.Text;
  if (!File.Exists(inFileName))
  { //Valid file wasn't entered.
    MessageBox.Show(inFileName + "does not exist." +   
        "Please select an existing file to convert.");
    return;
  }
  // Get the myItem object from the selected 
  // item in the save as combo box.
  myItem tmpItem = 
         cmbBoxOutput.SelectedItem as myItem;

  //Set some vars
  object fileName = inFileName; 
  object fileSaveName = inFileName.Substring(0,
                       inFileName.LastIndexOf("."))  
                       + tmpItem.ItemExtension; //".txt";
  object  vk_read_only  = false;
  object  vk_visible  = true;
  object  vk_true    = true;
  object  vk_false    = false; 
  object  vk_dynamic  = 2;

  object missing = System.Reflection.Missing.Value; 
  // the way to handle parameters you 
  // don't care about in .NET 
  object  vk_range = missing;
  object  vk_to = missing;
  object  vk_from = missing;
  Microsoft.Office.Interop.Word.ApplicationClass vk_word_app = 
           new Microsoft.Office.Interop.Word.ApplicationClass();
            
  // Open the document that was chosen by the dialog 
  Microsoft.Office.Interop.Word.Document aDoc = null;
  try
  {
    aDoc = vk_word_app.Documents.Open(
                 ref fileName, ref missing, 
                 ref vk_read_only, ref missing, 
                 ref missing, ref missing, 
                 ref missing, ref missing, 
                 ref missing, ref missing, 
                 ref missing, ref vk_visible, 
                 ref missing, ref missing, 
                 ref missing, ref missing );                 
  }
  catch (System.Exception ex)
  { 
                
    MessageBox.Show("There was a problem opening "+
                  fileName +" error:"+ex.ToString());
  }

  // Save the doc as the format requested file
  try
  {

  //Get the word saveas format from 
  //the myItem object we got from the 
  //save as combo box selected item
  object vk_saveformat = tmpItem.ItemWord;
            
  aDoc.SaveAs(ref fileSaveName, ref vk_saveformat, 
       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 (System.Exception ex)
  { MessageBox.Show("Error : "+ex.ToString());}
  finally
  { //Don't forget to close everything up...
    if (aDoc != null)
    {
      aDoc.Close(ref vk_false, ref missing, ref missing);
    }
    vk_word_app.Quit(ref vk_false,ref missing,ref missing);
  }
}

The main thing that does the work is a Word SaveFormat.

C#
Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatRTF

Under WdSaveFormat you have all the available formats that you would see in Word when you do a SaveAs. I am storing this in the ComboBox in the myItem class.

C#
public class myItem
{
  //vars to hold the property
  private String _itemName; 
  private String _itemExtension;
  private Object _itemWord;

  ...

  //This is the work saveas format object
  public Object ItemWord 
  {
    get{ return _itemWord; }
    set{ _itemWord = value; }
  }

  // This is what causes the combo box 
  // name to show up correctly.
  public override String ToString() 
  {
    return ItemName.ToString ();
  }
  //The constructors
  public myItem(){}

  public myItem(String inName, 
           String inExtension, Object inWordType)
  { // This allows us to set the properties 
    // when the object is created.
    ItemName = inName;
    ItemExtension = inExtension;
    ItemWord = inWordType;
  }
}

Loading the ComboBox looks like this:

C#
cmbBoxOutput.Items.Add(new myItem("XML Doc *.xml)", ".xml", 
    Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatXML));

Conclusion

So this is a pretty simple solution. It is important to note that when you save to XML you will get the word formatting stuff included in the XML. Some of that may not be what you want in your XML doc. Still the solution works well for converting RTF format to TXT.

My thanks to all the other CodeProject articles on Word automation that helped me put this solution together.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
I started my programmer career over 26 years ago doing COBOL and SAS on a MVS mainframe. It didn't take long for me to move into windows programming. I started my windows programming in Delphi (Pascal) with a Microsoft SQL server back end. I started working with vb.net when the beta 2 came out in 2001. After spending most of my programming life as a windows programmer I started to check out asp.net in 2004. I achieved my MCSD.net in April 2005. I have done a lot of MS SQL database stuff. I have a lot of experience with Window Service and Web services as well. I spent three years as a consultant programing in C#. I really enjoyed it and found the switch between vb.net and C# to be mostly syntax. In my current position I am programming in C# working on WPF and MSSql database stuff. Lately I have been using VS2019.

On a personal note I am a born again Christian, if anyone has any questions about what it means to have a right relationship with God or if you have questions about who Jesus Christ is, send me an e-mail. ben.kubicek[at]netzero[dot]com You need to replace the [at] with @ and [dot] with . for the email to work. My relationship with God gives purpose and meaning to my life.

Comments and Discussions

 
GeneralMy vote of 5 Pin
li_jing_ok8-Apr-12 14:59
li_jing_ok8-Apr-12 14:59 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey9-Feb-12 3:04
professionalManoj Kumar Choubey9-Feb-12 3:04 
GeneralMy vote of 5 Pin
ykurok19-Feb-11 22:23
ykurok19-Feb-11 22:23 
QuestionUpdated Microsoft Word 14.0 Object Library Pin
Petow8-Sep-10 2:08
Petow8-Sep-10 2:08 
AnswerRe: Updated Microsoft Word 14.0 Object Library Pin
kubben8-Sep-10 4:16
kubben8-Sep-10 4:16 
QuestionInterop word dll property if document has changed Pin
jemino23-Mar-09 22:49
jemino23-Mar-09 22:49 
Hi all, I'm using the interop dll library into my application to show a word document, I need to save it if changed. There is a property that indicates if file has changed?

thanks, Bruce
AnswerRe: Interop word dll property if document has changed Pin
kubben24-Mar-09 1:03
kubben24-Mar-09 1:03 
GeneralEasier get test if you really just want text Pin
mhorowit5-Feb-09 14:52
mhorowit5-Feb-09 14:52 
GeneralRe: Easier get test if you really just want text Pin
kubben5-Feb-09 14:59
kubben5-Feb-09 14:59 
QuestionRTF to DOC Pin
Mushtaque Nizamani7-Jun-08 18:42
Mushtaque Nizamani7-Jun-08 18:42 
AnswerRe: RTF to DOC Pin
kubben8-Jun-08 1:24
kubben8-Jun-08 1:24 
GeneralRe: Unicode Support in RTF Pin
Karthikeyan .g26-Mar-08 3:11
Karthikeyan .g26-Mar-08 3:11 
GeneralRe: Unicode Support in RTF Pin
kubben26-Mar-08 3:40
kubben26-Mar-08 3:40 
GeneralRe: Unicode Support in RTF Pin
Karthikeyan .g26-Mar-08 19:13
Karthikeyan .g26-Mar-08 19:13 
Generalgetting an inerop error Pin
sk840528-Aug-07 21:03
sk840528-Aug-07 21:03 
GeneralConverting to PDF Pin
srujana_m5-Aug-07 21:34
srujana_m5-Aug-07 21:34 
GeneralRe: Converting to PDF Pin
kubben6-Aug-07 1:39
kubben6-Aug-07 1:39 
GeneralRe: Converting to PDF Pin
srujana_m6-Aug-07 1:46
srujana_m6-Aug-07 1:46 
GeneralRe: Converting to PDF Pin
kubben6-Aug-07 1:50
kubben6-Aug-07 1:50 
GeneralRe: Converting to PDF Pin
Dreams...17-Sep-07 22:58
Dreams...17-Sep-07 22:58 
GeneralPrompt Rejected By Callee error message Pin
chinho8016-Apr-07 16:38
chinho8016-Apr-07 16:38 
GeneralRe: Prompt Rejected By Callee error message Pin
kubben17-Apr-07 2:10
kubben17-Apr-07 2:10 
GeneralRe: Prompt Rejected By Callee error message Pin
chinho8017-Apr-07 17:09
chinho8017-Apr-07 17:09 
GeneralRe: Prompt Rejected By Callee error message Pin
kubben18-Apr-07 2:10
kubben18-Apr-07 2:10 
GeneralGetting Interop Error Pin
gabrousman4-Jan-07 7:55
gabrousman4-Jan-07 7:55 

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.