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();
Former memberPosted Oct 4, 2013, 4:42 AM
you can also read your word file , either doc or docx file, by using this C# Library for Word: http://www.aspose.com/.net/word-component.aspx
sowbhagya arunaPosted Oct 11, 2012, 7:46 AM
// Create an instance of Word if (!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 file lpDisp = 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; }
sowbhagya arunaPosted Oct 11, 2012, 7:46 AM
// Create an instance of Word if (!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 file lpDisp = 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; }
Former membereditedPosted Jul 23, 2012, 11:45 PMEdited Jul 23, 2012, 11:46 PM
Great articles, I used Spire.Doc, easy to read word document and save to pdf document. hope help to everyone.
Priyadarshini BPosted Jun 28, 2012, 12:41 AM
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
Deego GKPosted May 3, 2012, 6:56 AM
It was wonderful page which helps me a lot
Deego GKPosted May 3, 2012, 6:56 AM
It was wonderful page which helps me a lot
nithyaPosted Mar 1, 2012, 6:13 AM
thanks dude
guru raJANPosted Feb 28, 2012, 7:23 AM
You have mentioned a name space that using Office; But i have used using Microsoft.office; Then only it works
sudeep vajralaPosted Dec 29, 2011, 7:17 AM
And when I publish the website in IIS 7 and ran it I am getting below error when trying to access Applicaiton.ActiveDocument. "This command is not available because no document is open". seems like the Application.Documents.Open is not reading the word document when running from IIS.
majed jemmiehPosted Oct 10, 2011, 3:26 PM
Thank you
destar johnPosted Jul 7, 2011, 1:40 AM
hey that has so many error.before you upload it you have to clean that error thanks
rrrPosted Feb 26, 2011, 4:31 PM
Thanks for the sample, it's help me. my Q: I want to raed only one line each time that I read from the document, does word.dll has a function such as StreamReader.ReadLine()? I need that in order to keep each line on data base.
Suresh RPosted Jan 24, 2011, 7:34 AM
Thank you...its really help full for me!
madura shanthaPosted Nov 3, 2010, 1:48 AM
Thanks man...your article is simple and clear.
Sar SoeurngPosted Sep 13, 2010, 9:38 PM
thank for your source code.Now I want you to help me to create a dictionary that easy to search definition of a word.
nitesh kapoorPosted Aug 21, 2010, 4:32 PM
I want to deply my application on web server . application is made for some recruitement consultant . Do you have any idea of licensing term in which i need only doc reader .
nitesh kapoorPosted Aug 21, 2010, 4:27 PM
I am not able to read header information for .doc files .
NVK PRASADPosted Jun 27, 2010, 5:18 AM
Very nice article
KasuneditedPosted May 7, 2010, 2:49 AMEdited Aug 25, 2010, 9:54 PM
I am developing system for filter emails from microsoft outlook. so i try to use ur above attached programe for read word documents but i was failed. coz it tells following error messages Error 2 The type 'Microsoft.Office.Interop.Word.ApplicationClass' exists in both 'c:\Windows\assembly\GAC\Microsoft.Office.Interop.Word \12.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.Word.dll' and 'd:\MIT\project\reading word and PDF\ReadWordDocument\ReadWordDocument\obj\Debug\Interop.Microsoft.Office.Interop.Word.dll' D:\reading word and PDF\ReadWordDocument\ReadWordDocument\Form1.cs 159 34 ReadWordDocument do u have any idea about error ... i am using VSTO 2008 and having Microsoft office object library 12.0
Le Nguyen Kim XuyenPosted Mar 26, 2010, 11:25 AM
Hi, I am doing a project related with MS Word 2007. I want to copy from a Word document to a new one but still keep the page layout of it (theme, page background...). Do you know how to do it? Please tell me with the following email: Email: [email protected] Thanks so much!
Sivakami RamasamyPosted Mar 26, 2010, 2:49 AM
Hi Krishnana, I am getting com expection. How can i solve the solve. please do needful.
nazmi dumanPosted Mar 10, 2010, 4:36 PM
Thank you Beatifull
Zaheer MazharPosted Mar 4, 2010, 2:20 AM
Thanks for this very informative tutorial.....
sandip PatelPosted Dec 19, 2009, 12:12 AM
how to used in vb.net which use of there and references reply as soon as possible
perumal mPosted Dec 18, 2009, 11:29 PM
i am creating a windows application in microsoft visual studio using C# code.i would like to upload a word document in my project. i dont know the tool and c# code.please sent me the tool and C# code
Kartika AngkawijayaPosted Sep 7, 2009, 5:22 AM
Hi Krishnan, Do you have any idea how to count images inside the word document after opening the Word file? I downloaded your code (thanks a lot for that), then I modified a little. What I was trying to do is, calculating the objects inside the opened document, ie. sentences, paragraphs, etc., which can be done easily by accessing the word object. But I'm stuck at the moment on how to find the image object. Maybe it's not fully related to this topic, but I do really hope that you can help me or give some hints :) Thank you very much, confused .net newbie - Kartika
Nguyen 0Posted Aug 25, 2009, 11:23 PM
I have problem with vietnamese. i use unicode font.
Kapil YadavPosted Aug 12, 2009, 6:43 AM
Thanks Krishnan for the article But this assembll needs microsoft word to be installed on the machine . As i am using asp.net application on an external server where it is not possible to install word . so is there any solution . Thanks and Best Regards Kapil
malyadri sPosted Aug 10, 2009, 1:03 AM
'((Microsoft.Office.Interop.Word.ApplicationClass)(oWordApp)).ActiveDocument' threw an exception of type 'System.Runtime.InteropServices.COMException' i am trying to work on opened word document events like documentbeforeprint i writen this lines of code button_click { object fileName = strSeverPath; object readOnly = true; object isVisible = true; object password = "abc"; oWordDoc = oWordApp.Documents.Open(ref fileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p> wordApp.Visbule =true; //beforeprint event call heare oWordApp.DocumentBeforePrint += new Events4_DocumentBeforePrintEventHandler(oWordApp_DocumentBeforePrint); } void oWordApp_DocumentBeforePrint(Document Doc, ref bool Cancel) { //throw new NotImplementedException(); // here i am write some lines of code is not working but event fire oWordApp._CodeName.ToString(); } please give solution Regards, Malyadri </o:p>
gowrisanker PPosted Jun 22, 2009, 12:32 AM
hi i tried the same coding in web application Clipboard.GetDataObject() is returning null value. when i did the manual paste action in new word file, it shows the contents of the source in the same format. could you please help me to solve this issue?
gowrisanker PPosted Jun 22, 2009, 12:27 AM
hi i tried this same coding in web application but this Clipboard.GetDataObject() is returning null value when i did manual paste action in word, it is showing all the contents of the source in the same format could you please help me to resolve the issue
rupal rathiPosted Mar 20, 2009, 9:05 AM
IDataObject data=Clipboard.GetDataObject(); how I will get this one
palani pPosted Dec 18, 2008, 1:29 AM
Hello sir, I jsct run your application the following error found.... Error 1 No overload for method 'Open' takes '12' arguments ~\ReadWordDocument\ReadWordDocument\Form1.cs 160 49 ReadWordDocument pls send solution..
Satish JadhaveditedPosted Nov 29, 2008, 12:04 AMEdited Aug 25, 2010, 9:53 PM
e
jai mPosted Aug 14, 2008, 5:05 AM
Hi, I want to know how to Import, using word; using office; namespaces in c# windows applications.its giving me error for these namespaces
jai mPosted Aug 14, 2008, 5:03 AM
Hi, I want to know how to Import, using word; using office; namespaces in c# windows applications.its giving me error for these namespaces
jai mPosted Aug 14, 2008, 5:03 AM
Hi, I want to know how to Import, using word; using office; namespaces in c# windows applications.its giving me error for these namespaces
jai mPosted Aug 14, 2008, 5:02 AM
Hi, I want to know how to Import, using word; using office; namespaces in c# windows applications.its giving me error for these namespaces
jai mPosted Aug 14, 2008, 5:01 AM
Hi, I want to know how to Import, using word; using office; namespaces in c# windows applications.its giving me error for these namespaces
jai mPosted Aug 14, 2008, 5:01 AM
Hi, I want to know how to Import, using word; using office; namespaces in c# windows applications.its giving me error for these namespaces
Gaurang GanatraPosted Jan 14, 2008, 12:23 AM
Hi Krishnan, I was wondering whether i would be able to read a mobile word document in the windows mobile. Is there a third party library which can enable me to search for a word in a particular word document in compact framework?
pipaer pipaerPosted Nov 20, 2007, 10:45 AM
This prog is missing four Parameters, The funtction of the documents.open should have 16 parameters.
pipaer pipaerPosted Nov 20, 2007, 9:58 AM
The file of this ReadWordDocument.zip can't be run! Documents.Open is bug
swapnajeet choudhuryPosted Aug 31, 2007, 10:44 AM
the solution provided holds good for windows application but it is not working for webpages.plz provide me th solution to read a word document in a web page.i want to read the word document in a textbox.can i do it?
Shubhi MishraPosted Jul 18, 2007, 5:51 AM
Hello. I have a word pad document. In this document i would like to read a character "|", which occurs many times in this document. It would be very kind of you if you can assist me with the above. Thank you
Anju BhatiaPosted Jul 5, 2007, 3:15 AM
Hi I am using ur code but it is giving error at the line txtFileContent.Text=data.GetData(DataFormats.Text).ToString(); that Object reference not set to an instance of an object. pls help me..its urgent Anju Bhatia Pune
Tim AlvordPosted Feb 14, 2007, 6:58 PM
Krishnan, Great code! I have a question and a bug. The question is how do I prevent Word from displaying on the wordApp.Documents.Open() call. Second, the bug. If the Word file contains a macro, the code crashes on the doc.ActiveWindow.Selection.WholeStory() call... Thanks, Tim
pieskaPosted Feb 14, 2007, 11:39 AM
Where it can be found to use your code example? Is it some kind of SDK dll-file?
IndhuPosted Jul 5, 2006, 1:19 AM
What logic have you applied in word.dll and Office.dll in WordDocument program? I have understood the basic concept of your program. Kindly, explain logic behind word.dll and Office.dll.
sudheer kollaramPosted Jun 16, 2006, 1:55 AM
hello sir i am doign one project.In that i want to put a textbox with multipul line. i want to give options like fornt name,size,bold,italic.... like msword plz help me to do thing thing thanking you sudheerkollaram
Joseph BaileyPosted Mar 14, 2006, 1:00 AM
Do your examples for generating reports in Excel and Word require the Visual Studio Tools for Office edition? I recently purchased (and am still awaiting) Visual Studio Professional Edition and it appears it does not come with the Office Interoperability suite that VSTO does.