Reader Level:
ARTICLE

Reading a word document using C#

Posted by Krishnan LN Articles | Visual C# February 27, 2006
This tutorial is basically for C# beginners. It describes how to read a word document using C#.
  • 2
  • 0
  • 262657
Download Files:
 

We may have used FileStream to read text from a text file but not the same way for getting text from a word document.

We have to use a Microsoft COM component called "Microsoft Word 9.0 object library" which provides classes and methods to read from a word document.

We have to use Word.ApplicationClass to have access to the word application.

Open the word document in memory, copy all the content to the clipboard and then we can take the data from the clipboard.

The code required is given below:

Word.ApplicationClass wordApp=new ApplicationClass();

object file=path;

object nullobj=System.Reflection.Missing.Value;  

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);

doc.ActiveWindow.Selection.WholeStory();

doc.ActiveWindow.Selection.Copy();

IDataObject data=Clipboard.GetDataObject();

txtFileContent.Text=data.GetData(DataFormats.Text).ToString();

doc.Close();

Article Extensions
Contents added by adil khan on Sep 24, 2012
Contents added by thai ho on Mar 13, 2010

hi Krishnan.L.N your ex project is very good, but can you keep read the document and keep it's format and display it's format

COMMENT USING

Trending up