Read Text from Word documents
In this section we will discuss how to read text from the Word document.
Step 1
Add Microsoft.Office.Interop.Word assembly to project. Please refer the following snapshot.
In this section we will discuss how to read text from the Word document.
Step 1
Add Microsoft.Office.Interop.Word assembly to project. Please refer the following snapshot.
Step 2
After adding assembly, please add following namespace to class/code behind files.
After adding assembly, please add following namespace to class/code behind files.
- using Microsoft.Office.Interop.Word;
Then write the following code read text from Word documents which returns content as a string.
- /// </summary>
- /// <returns></returns>
- private string GetTextFromWord()
- {
- StringBuilder text = new StringBuilder();
- Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
- object miss = System.Reflection.Missing.Value;
- object path = @"D:\Articles2.docx";
- object readOnly = true;
- Microsoft.Office.Interop.Word.Document docs = word.Documents.Open(ref path, ref miss, ref readOnly, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
- for (int i = 0; i < docs.Paragraphs.Count; i++)
- {
- text.Append(" \r\n " + docs.Paragraphs[i + 1].Range.Text.ToString());
- }
- return text.ToString();
- }
tham khảo tại đây.