Pages

Friday, September 18, 2009

Reading a word file Using c#

Reading a word file in c# is very simple we just need to add the reference of the COM component in our application. Click on Add >> Reference and Select the COM tab, under that tab please select the Microsoft object library 9.0 or higher. and after this use the following code to read any file...


Microsoft.Office.Interop.Word.ApplicationClass wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
object file = "D:\\Anil\\test\\testfile.doc"; // path for word file
object nullobj = System.Reflection.Missing.Value;
Microsoft.
Office.Interop.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);
doc.
ActiveWindow.Selection.WholeStory();
doc.
ActiveWindow.Selection.Copy();
IDataObject data
= Clipboard.GetDataObject();
string allText = data.GetData(DataFormats.Text).ToString();
doc.
Close(ref nullobj, ref nullobj, ref nullobj);
wordApp.
Quit(ref nullobj, ref nullobj, ref nullobj);
Textbox1.
Text = allText);


Happy Coading!!!

Thanks
Anil Kumar Pandey
System Architect
Mumbai, Maharshtra

7 comments:

  1. Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 80070005.

    I m getting this error while running the same code on server !! so what should I do ? is there any configuration i need to do ?

    ReplyDelete
  2. This may be the issue of different version of Windows as 32bit and 64 bit. If you are using a web config file place this.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. The above error occurs bcoz ms office doesnt install on server. That is the only reason as i am also facing same problem.

    ReplyDelete
  5. IN that case the DLL or the COM must be register there. If you had used that inside the BIN the error will be gone.
    Make use if it to remove the error.

    ReplyDelete
  6. is it possible only in windows form??????
    plz help me.

    ReplyDelete
  7. @navaneethan no it can be used in asp apps as well

    ReplyDelete

Kontera