how to read MS word files to rich textbox...
using streams i can't achieve this...
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Swapnil Palande15 reputationPosted Jul 18, 2009, 12:27 AM EDT
Do the following steps.
1. first add reference to "Microsoft Word Object Library"
2. add following line at top in the code file
using System.IO;
using Microsoft.Office.Interop;
3. Now add following code in your code file (I have write this code on button click)
private void btnLoadFile_Click(object sender, EventArgs e)
{
string str;
OpenFileDialog openfiledg = new OpenFileDialog();
openfiledg.Filter = "Word Document (*.doc)|*.doc|Text File (*.txt)|*.txt|All Files (*.*)|*.*";
openfiledg.FilterIndex = 2;
openfiledg.RestoreDirectory = true;
if ((openfiledg.ShowDialog() == DialogResult.OK && openfiledg.FileName.Length > 0))
{
IDataObject Data1;
object nullobj, file;
Microsoft.Office.Interop.Word.Document wordDoc;
Microsoft.Office.Interop.Word.ApplicationClass WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
nullobj = System.Reflection.Missing.Value;
file = openfiledg.FileName;
wordDoc = WordApp.Documents.Open(ref file, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj);
wordDoc.ActiveWindow.Selection.WholeStory();
wordDoc.ActiveWindow.Selection.Copy();
Data1 = Clipboard.GetDataObject();
str = Data1.GetData(DataFormats.Rtf).ToString();
richTextBox1.Rtf = str;
wordDoc.Close(ref nullobj, ref nullobj, ref nullobj);
}
I got this code from following link:
http://bytes.com/groups/net-c/522560-load-word-file-html-file-into-richtextbox
Hope it will solve your problem.
Regards,
Swapnil.