---
title: "Read from word document line by line"  
description: "Read from word document line by line"  
author: "marcel ethan"  
published: 2014-01-25  
updated: 2014-01-25  
canonical: https://www.mindstick.com/forum/1869/read-from-word-document-line-by-line  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# Read from word document line by line

I'm trying to read a [word document](https://www.mindstick.com/forum/159153/how-to-find-and-replace-in-a-word-document-using-vba) using C#. I am able to get all text but I want to be able to read [line by line](https://www.mindstick.com/interview/34101/how-do-you-read-a-file-line-by-line-using-a-generator-like-approach-e-g-yield-return) 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](https://www.mindstick.com/forum/34615/software-framework-vs-library) to read the file. Below is my code [till now](https://answers.mindstick.com/qa/40087/which-is-the-best-indian-tv-serial-till-now):

[Application](https://www.mindstick.com/articles/12824/calculator-application-in-android) word = new Application();

Document doc = new Document();

object [fileName](https://www.mindstick.com/interview/23463/difference-between-include-filename-and-include-filename) = path;

// Define an object to pass to the API for [missing](https://answers.mindstick.com/qa/52138/international-missing-children-s-day-2019-was-observed-on) [parameters](https://www.mindstick.com/articles/87/passing-parameters-in-c-sharp)

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](https://www.mindstick.com/forum/33870/how-parallel-foreach-works-internally) (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.

## Replies

### Reply by Pravesh Singh

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();
```


---

Original Source: https://www.mindstick.com/forum/1869/read-from-word-document-line-by-line

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
