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#.
Reader Level:
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();

Login to add your contents and source code to this article
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

post comment
     

// Create an instance of Wordif (!objWordApp.CreateDispatch("Word.Application")) {SendMessage(m_pFRMgr->m_hActiveDlg,WM_TR_READ,0,(LONG)(LPCSTR)m_pFRMgr->m_strErrMsg);}objWordApp.SetVisible(FALSE);objDocs = objWordApp.GetDocuments();//open the word filelpDisp = objDocs.Open( COleVariant(strTRFile, VT_BSTR), covFalse, covFalse, covFalse, covOptional, covOptional, covFalse, covOptional, covOptional, covOptional );if(lpDisp == NULL){if(objWordApp)objWordApp.Quit(covFalse, covFalse, covFalse);}objDoc.AttachDispatch(lpDisp);//Get word Source Doc ID: objWords = objDoc.GetWords();int sizeWords = objWords.GetCount();CString strDocID;bool bSource = false;for(int cntWords=1; cntWords <= sizeWords; cntWords++){Range objRange1;CString strWord;objRange1= objWords.Item(cntWords);strWord = objRange1.GetText();strWord.TrimLeft();strWord.TrimRight();if(strWord.CompareNoCase("Time") == 0)break;if(bSource && strWord.CompareNoCase(":") != 0)strDocID += strWord;}

Posted by sowbhagya aruna Oct 11, 2012

// Create an instance of Wordif (!objWordApp.CreateDispatch("Word.Application")) {SendMessage(m_pFRMgr->m_hActiveDlg,WM_TR_READ,0,(LONG)(LPCSTR)m_pFRMgr->m_strErrMsg);}objWordApp.SetVisible(FALSE);objDocs = objWordApp.GetDocuments();//open the word filelpDisp = objDocs.Open( COleVariant(strTRFile, VT_BSTR), covFalse, covFalse, covFalse, covOptional, covOptional, covFalse, covOptional, covOptional, covOptional );if(lpDisp == NULL){if(objWordApp)objWordApp.Quit(covFalse, covFalse, covFalse);}objDoc.AttachDispatch(lpDisp);//Get word Source Doc ID: objWords = objDoc.GetWords();int sizeWords = objWords.GetCount();CString strDocID;bool bSource = false;for(int cntWords=1; cntWords <= sizeWords; cntWords++){Range objRange1;CString strWord;objRange1= objWords.Item(cntWords);strWord = objRange1.GetText();strWord.TrimLeft();strWord.TrimRight();if(strWord.CompareNoCase("Time") == 0)break;if(bSource && strWord.CompareNoCase(":") != 0)strDocID += strWord;}

Posted by sowbhagya aruna Oct 11, 2012

Great articles, I used Spire.Doc, easy to read word document and save to pdf document. hope help to everyone.

Posted by jack wen Jul 23, 2012

Great post. But this works only when MS office is installed. There is a free dotnet library that works even when MS office is not installed on your machine. Check this out. http://programmingfree.blogspot.in/2012/06/working-with-word-documents.html

Posted by Priyadarshini B Jun 28, 2012

It was wonderful page which helps me a lot

Posted by Deego GK May 03, 2012
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
Join a Chapter
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.