C # RichTextBox in Visual Studio 2005 saves the file in the RTF Format
Savefiledialog savefile1 = new savefiledialog ();
// Initialize the savefiledialog to specify the RTF extention for the file.
Savefile1.defaultext = "*. rtf ";
Savefile1.filter = "RTF files | *. rtf ";
// Determine whether the user selected a file name from the savefiledialog.
If (savefile1.showdialog () = system. Windows. Forms. dialogresult. OK & amp &&
Savefile1.filename. length> 0)
{
// Save the contents of the RichTextBox into the file.
Richtextbox1.savefile (savefile1.filename );
}
RichTextBox text is saved in Word format:
Private void button#click (Object sender, system. eventargs E)
... {// Save As a word file
If (this. richtextbox1.text = "")
Return;
If (this. savefiledialog1.showdialog () = dialogresult. Cancel)
Return;
String filename = This. savefiledialog1.filename;
If (filename. Length <1)
Return;
Filename + = ". Doc ";
Try
...{
Word. applicationclass myword = new word. applicationclass ();
Word. Document mydoc;
Object nothing = system. reflection. Missing. value;
Mydoc = myword. Documents. Add (ref nothing, ref nothing );
Mydoc. Paragraphs. Last. range. Text = This. richtextbox1.text;
Object myfilename = filename;
// Save the content of the worddoc object as a doc object
// Mydoc. saveas (ref myfilename, ref nothing, ref nothing, ref nothing, ref nothing );
// Close the worddoc Document Object
Mydoc. Close (ref nothing, ref nothing, ref nothing );
// Close the wordapp Component Object
Myword. Quit (ref nothing, ref nothing, ref nothing );
MessageBox. Show ("Word file saved successfully", "message prompt", messageboxbuttons. OK, messageboxicon. information );
}
Catch (exception ERR)
...{
MessageBox. Show ("An error occurred while saving the Word file! "+ Err. message," message prompt ", messageboxbuttons. OK, messageboxicon. information );
}
}