Appear in Online Interview to test your skills and be ready for job!
Twitter Twitter LinkedIn YouTube Google
Be Interview ready ! | Search
Submit content and win cash & prizes monthly.
Winners

Win Prizes

Announcements
Like us on Facebook
Sponsor
Top Forums Author
Thu, 24-Apr-2014 Authors
Bageshkumarbagi
10
Chaithragm
10
All Time Authors
Jayakumars
3930
Karthikanbarasan
3455
Chvrsri
3140

Latest members | More ...


(Statistics delayed by 5 minutes)

Rich Textbox (RTF) to Word (.docx) file - winforms

Manjumh
Posted by Manjumh under C# on 2/11/2012 5:17:40 AM | Points: 10 | Views : 13707 | Status : [Member] | Replies : 11
Hi Everyone,

I want the code (c# windows application) that should store the content of the "Rich text box" control in a word file(.docx).
I am not getting the proper solusion please help me (Very urgent).

thank you,
Manjunath



Reply | Reply with Attachment

=> Scroll below for responses


Comments or Responses

Posted by: Gow.net on: 2/11/2012 [Member] Starter | Points: 25

hi sir try this coding
using (System.IO.StreamReader sr = new System.IO.StreamReader("TestFile.txt"))

{
RichTextBox1.Text = sr.ReadToEnd();
}

or
OpenFileDialog f = new OpenFileDialog();

f.Title = "open file as..";
f.Filter = "Doc Files|*.doc|Java Files|*.java|C# Files|*.cs|All Files|*.*"; // and in a similar way you can load any format here.......
DialogResult dr = f.ShowDialog();
if (dr == DialogResult.OK)
{
s1 = f.FileName;
richTextBox1.LoadFile(s1);
open=true;
}

gowthaman8870226416

Manjumh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Manjumh on: 2/13/2012 [Member] Starter | Points: 25

Hello sir,

Thank you for your reply,
actually i need code to convert the content of "Rich Text Box" to ".docx" file,
but your solution is reverse of it.

Regards,
Manjunath

Manjumh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Pradeepkumar417 on: 2/13/2012 [Member] Starter | Points: 25

Hi,

by using SaveFile() method you can save data from richtextbox to msword document.

RichTextBox rtxt = new RichTextBox ();
rtxt.SaveFile("text.docx");

Thanks & Regards,
Software Engineer,
Pradeep Kumar

Manjumh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Manjumh on: 2/13/2012 [Member] Starter | Points: 25

No sir, it will not work for .docx file format.
It will create the .docx file but when we try to open it in microsoft word, it say "The file text.docx cannot be opened because there are problems with the content" (file is corrupt and cannot be opened) :(

Thanks,
Manjunath

Manjumh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Naraayanan on: 2/13/2012 [Member] Starter | Points: 25

Hi,
Check this link
http://stackoverflow.com/questions/4903304/rtf-to-docx-conversion-in-c-sharp-net
http://www.c-sharpcorner.com/uploadfile/mahesh/richtextbox-in-C-Sharp/

Regards,
Lakshmi Naraayanan.S
http://dotnettechrocks.blogspot.in/
http://abaprocker.blogspot.com/

Manjumh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Manjumh on: 2/14/2012 [Member] Starter | Points: 25

yes i tried the above urls, In that also i am getting the same exception "file is corrupt and cannot be opened" for .docx file.

Thanks,
Manjunath

Manjumh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Mayankshri on: 2/14/2012 [Member] Starter | Points: 25

use this code-

try
{
object missing = System.Reflection.Missing.Value;
object Visible = true;
object start1 = 0;
object end1 = 0;

Microsoft.Office.Interop.Word.ApplicationClass WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
Microsoft.Office.Interop.Word.Document adoc = WordApp.Documents.Add(ref missing, ref missing, ref missing, ref missing);
Microsoft.Office.Interop.Word.Range rng = adoc.Range(ref start1, ref missing);
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.InitialDirectory = @"C:\";
saveFileDialog1.Title = "Save word Files";
saveFileDialog1.CheckFileExists = false;
saveFileDialog1.CheckPathExists = true;
saveFileDialog1.DefaultExt = "docx";
saveFileDialog1.Filter = "Docx files (*.docx)|*.doc|All files (*.*)|*.*";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.OverwritePrompt = true;
saveFileDialog1.RestoreDirectory = true;

if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
rng.Font.Name = "Calibri (Body)";
rng.InsertAfter(rtbenglishfile.Text);
object filename = saveFileDialog1.FileName;
adoc.SaveAs(ref filename, 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, ref missing);
//WordApp.Visible = true;
WordApp.Visible = false;
adoc.Close(ref missing, ref missing, ref missing);
WordApp.Quit(ref missing, ref missing, ref missing);
WordApp = null;
}
}
catch (Exception o)
{
MessageBox.Show(o.Message);
}


Manjumh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Manjumh on: 2/24/2012 [Member] Starter | Points: 25

Thank you all for your reply..

I solve the problem in different method, actually i am converting the richtextbox content(rtf) into to html and then storing it in .docx file.

Thanks again,
Manjunath


Manjumh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Mayankshri on: 2/24/2012 [Member] Starter | Points: 25

Hi,
can you please upload the code

Manjumh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Manjumh on: 2/24/2012 [Member] Starter | Points: 25

sure,

you can find it in below url,
http://www.codeproject.com/Articles/27431/Writing-Your-Own-RTF-Converter

i have altered the code according to my requirement.

Regards,
Manjunath

Manjumh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Cuuprum on: 12/31/2012 [Member] Starter | Points: 25

hello :)
thank a lot for this discussion, its really help me

Regrads,
faishal

Manjumh, if this helps please login to Mark As Answer. | Alert Moderator


Login to post response.

Comments / Responses
Select text & click toolbar to format. Formatting appears in viewmode only. HTML Tags are not allowed.
Bold Italic Underline Paragraph Title Code  Link 
 Wait ... Processing ..... please wait.

Found interesting? Add this to: