Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read from word document line by line


I'm trying to read a word document using C#. I am able to get all text but I want to be able to read line by line and store in a list and bind to a gridview. Currently my code returns a list of one item only with all text (not line by line as desired). I'm using the Microsoft.Office.Interop.Word library to read the file. Below is my code till now:

    Application word = new Application();
    Document doc = new Document();

    object fileName = path;
    // Define an object to pass to the API for missing parameters
    object missing = System.Type.Missing;
    doc = word.Documents.Open(ref fileName,
            ref missing, ref missing, ref missing, ref missing,
            ref missing, ref missing, ref missing, ref missing,
            ref missing, ref missing, ref missing, ref missing,
            ref missing, ref missing, ref missing);

    String read = string.Empty;
    List<string> data = new List<string>();
    foreach (Range tmpRange in doc.StoryRanges)
    {
        //read += tmpRange.Text + "<br>";
        data.Add(tmpRange.Text);
    }
    ((_Document)doc).Close();
    ((_Application)word).Quit();

    GridView1.DataSource = data;
    GridView1.DataBind();
like image 712
Bat_Programmer Avatar asked Sep 01 '13 03:09

Bat_Programmer


People also ask

Can you get a word document to read to you?

Read Aloud is only available for Office 2019, Office 2021, and Microsoft 365. On the Review tab, select Read Aloud. To play Read Aloud, select Play in in the controls. To pause Read Aloud, select Pause.

How do I get Microsoft narrator to read a word document?

To read the current word, press Narrator + K or Narrator + Ctrl + 5 (numeric keypad). When you press either command twice in a row, Narrator will spell the word. To read the next word, press Narrator + L or Narrator + Ctrl + Right arrow key.

What is immersive Reader in word?

Immersive Reader is a reading enhancement tool available in Office 365 that will work with Word, OneNote and Outlook. Immersive Reader will speak text in a choice of voices and allows you to view the text in a variety of fonts, sizes and background colours.


Video Answer


2 Answers

Ok. I found the solution here.


The final code is as follows:

Application word = new Application();
Document doc = new Document();

object fileName = path;
// Define an object to pass to the API for missing parameters
object missing = System.Type.Missing;
doc = word.Documents.Open(ref fileName,
        ref missing, ref missing, ref missing, ref missing,
        ref missing, ref missing, ref missing, ref missing,
        ref missing, ref missing, ref missing, ref missing,
        ref missing, ref missing, ref missing);

String read = string.Empty;
List<string> data = new List<string>();
for (int i = 0; i < doc.Paragraphs.Count; i++)
{
    string temp = doc.Paragraphs[i + 1].Range.Text.Trim();
    if (temp != string.Empty)
        data.Add(temp);
}
((_Document)doc).Close();
((_Application)word).Quit();

GridView1.DataSource = data;
GridView1.DataBind();
like image 93
Bat_Programmer Avatar answered Oct 02 '22 09:10

Bat_Programmer


The above code is correct, but it's too slow. I have improved the code, and it's much faster than the above one.

List<string> data = new List<string>();
Application app = new Application();
Document doc = app.Documents.Open(ref readFromPath);

foreach (Paragraph objParagraph in doc.Paragraphs)
    data.Add(objParagraph.Range.Text.Trim());

((_Document)doc).Close();
((_Application)app).Quit();
like image 33
Pratik Anjania Avatar answered Oct 02 '22 08:10

Pratik Anjania