Monday, June 29, 2009

C# Read text from .doc to c# application

Read text from word file (.doc)

Add referance to MicroSoft Word 11.0 object library

Word.ApplicationClass wordApp = new Word.ApplicationClass();
string filePath = "c:\\tempfile.doc";
object file = filePath;
object nullobj = System.Reflection.Missing.Value;
object nosave = Word.WdSaveOptions.wdDoNotSaveChanges;
Word.Document doc = 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);
string m_Content = doc.Content.Text;
doc.Close(ref nosave, ref nullobj, ref nullobj);
wordApp.Quit(ref nosave, ref nullobj, ref nullobj);

richTextBox.Text = m_Content;