Home > DeveloperSection > Forums > Read from word document line by line
marcel ethan
marcel ethan

Total Post:105

Points:735
Posted on    January-25-2014 1:21 AM

 C# C# 
Ratings:


 1 Reply(s)
 1055  View(s)
Rate this:

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();

Any help would be highly appreciated. Thanks.



Pravesh Singh

Total Post:411

Points:2881
Posted on    January-25-2014 1:45 AM

Hi Marcel,

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();


Don't want to miss updates? Please click the below button!

Follow MindStick