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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Swapnil PalandePosted Jul 18, 2009, 4:27 AM
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.