Storage of RichTextBox and RTF Files

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.