Click here to Skip to main content
15,886,093 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello ;

can anyone tell us. how to read a microsoft word document in c#.
Posted

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();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900