Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
hi,
How i can save the text of richtextbox to document(.doc) i use this code but not working:
C#
private void savebtn_Click(object sender, EventArgs e)
       {
           SaveFileDialog dr = new SaveFileDialog();
           dr.Filter = "Doc File|*.docx";
           if (dr.ShowDialog() == DialogResult.OK)
           {
               rtbWord.SaveFile(dr.FileName);
               MessageBox.Show("save successfully", "Address File : " + dr.FileName, MessageBoxButtons.OK, MessageBoxIcon.Information);
       }

this code save file but when execute the document file is not open help plz.
Posted

Hi ,

Please kindly find the code below that saving the Richtextbox text to .doc file


C#
// Create a SaveFileDialog to request a path and file name to save to.
          SaveFileDialog saveFile1 = new SaveFileDialog();

          // Initialize the SaveFileDialog to specify the RTF extension for the file.
          saveFile1.DefaultExt = "*.doc";
          saveFile1.Filter = "DOC Files|*.doc";

          // Determine if the user selected a file name from the saveFileDialog.
          if (saveFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK &&
             saveFile1.FileName.Length > 0)
          {
              // Save the contents of the RichTextBox into the file.
              richTextBox1.SaveFile(saveFile1.FileName, RichTextBoxStreamType.PlainText);
              MessageBox.Show("save successfully", "Address File : " + saveFile1.FileName, MessageBoxButtons.OK, MessageBoxIcon.Information);

          }


Regards,
A.Mandour
 
Share this answer
 
v2
Comments
amirmohamad 29-Sep-12 4:16am    
tank's man for your help
vote 5+
amirmohamad 29-Sep-12 4:37am    
and for open this code is true or not:
OpenFileDialog openfile1 = new OpenFileDialog();
openfile1.Filter = "Doc File|*.docx";
if (openfile1.ShowDialog() == DialogResult.OK)
{
rtbWord.LoadFile(openfile1.FileName);
MessageBox.Show("Done successfully", " ", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
AfterShave 29-Sep-12 6:32am    
I was thinking exactly of that code!
Hi,
this is Code to Open Word File(.doc) or Text File(.txt) file in Richtextbox

C#
OpenFileDialog openFD = new OpenFileDialog();
        string Chosen_File = "";

        openFD.InitialDirectory = "C:";
        openFD.Title = "Open a Text File";
        openFD.FileName = "";
        openFD.Filter = "Text Files|*.txt|Word Documents|*.doc";
        if (openFD.ShowDialog() != DialogResult.Cancel)
        {
            Chosen_File = openFD.FileName;
            richTextBox1.LoadFile(Chosen_File, RichTextBoxStreamType.PlainText);
        }


Regards,
Ahmed Mandour
 
Share this answer
 
Comments
amirmohamad 29-Sep-12 5:05am    
And tank's again man

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900