---
title: "Editing MS Word Document in CSharp .NET"  
description: "/* Style Definitions */   table.MsoNormalTable  	{mso-style-name:\"Table Normal\";  	mso-tstyle-rowband-size:0;  	mso-tstyle-colband-size:0;  	mso-st"  
author: "Uttam Misra"  
published: 2010-07-12  
updated: 2020-09-08  
canonical: https://www.mindstick.com/articles/58/editing-ms-word-document-in-csharp-dot-net  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# Editing MS Word Document in CSharp .NET

Here I am going to demonstrate how to create and edit MS Word file through C#.Net

##### Example

![Editing MS Word Document in CSharp .NET](https://www.mindstick.com/mindstickarticle/019723e0-ec6c-4c95-a381-f40efba920f8/images/335bf97a-56bf-40ef-a350-babf5fa94d0e.png)

To create or edit MS Word document, first of all we need to add two references to our project.

![Editing MS Word Document in CSharp .NET](https://www.mindstick.com/mindstickarticle/019723e0-ec6c-4c95-a381-f40efba920f8/images/fba10a1a-172c-4972-a15c-94bd6cc3d86e.png)

##### Coding

##### Creating document

```
 private void btnCreate_Click(object sender, EventArgs e)        {            try            {//creating object for missing value                object missing = System.Reflection.Missing.Value;//object for end of file                object endofdoc = "\\endofdoc"; //creating instance of word applicationMicrosoft.Office.Interop.Word._Application w = new Microsoft.Office.Interop.Word.Application();//creating instance of word document                Microsoft.Office.Interop.Word._Document doc;//setting status of application to visible                w.Visible = true;//creating new document    doc = w.Documents.Add(ref missing, ref missing, ref missing,     ref missing);//adding paragraph to document                Microsoft.Office.Interop.Word.Paragraph para1;                para1 = doc.Content.Paragraphs.Add(ref missing);                object styleHeading1 = "Heading 1";                para1.Range.set_Style(ref styleHeading1);                para1.Range.Text = "Heading One";                para1.Range.Font.Bold = 1;                para1.Format.SpaceAfter = 24;                para1.Range.InsertParagraphAfter();//creating second paragraph                 Microsoft.Office.Interop.Word.Paragraph para2;                para2 = doc.Content.Paragraphs.Add(ref missing);                    para2.Range.Text = "Heading OneHeading OneHeading OneHeading OneHeading OneHeading OneHeading"+ '\n'+"OneHeading OneHeading OneHeading OneHeading OneHeading OneHeading One";                para2.Range.Font.Bold = 1;                para2.Format.SpaceAfter = 24;                para2.Range.InsertParagraphAfter();             }            catch (Exception ex)            {                 MessageBox.Show(ex.Message);            }        }
```

##### Editing document

```
        private void btnEdit_Click(object sender, EventArgs e)        { //creating instance of word application     Microsoft.Office.Interop.Word._Application w = new Microsoft.Office.Interop.Word.Application();                                object path = @"C:\table.doc";                object read = "ReadWrite";                object readOnly = false;                object o = System.Reflection.Missing.Value;//opening document    Microsoft.Office.Interop.Word._Document oDoc = w.Documents.Open(ref path, ref o, ref readOnly, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o);                 try                {               //loop for each paragraph in documentforeach (Microsoft.Office.Interop.Word.Paragraph p in oDoc.Paragraphs)                {                    Microsoft.Office.Interop.Word.Range rng = p.Range;  Microsoft.Office.Interop.Word.Style styl = rng.get_Style() as Microsoft.Office.Interop.Word.Style;                    //checking if document containg tableif ((bool)rng.get_Information (Microsoft.Office.Interop.Word.WdInformation.wdWithInTable)                    == true)                    {//loop for each cell in table   foreach (Microsoft.Office.Interop.Word.Cell c in rng.Cells)                        {                            if (rng.Cells.Count > 0)                            {            //checking for desired field in table                                if (c.Range.Text.ToString().Contains("ID"))                  //editing values in tables.                                    c.Next.Range.Text = "1";                                if (c.Range.Text.ToString().Contains("Name"))                                     c.Next.Range.Text = "Haider";  if (c.Range.Text.ToString().Contains("Address"))                                    c.Next.Range.Text = "Allahabad";                                                 }                        }//saving document                        oDoc.Save();                    }                                   }//closing document                oDoc.Close(ref o,ref o,ref o);            }            catch (Exception ex)            {                oDoc.Close(ref o, ref o, ref o);                 MessageBox.Show(ex.Message);            }                    }
```

##### Screen shot

##### Before editing

![Editing MS Word Document in CSharp .NET](https://www.mindstick.com/mindstickarticle/019723e0-ec6c-4c95-a381-f40efba920f8/images/31668c8a-8236-495d-8b24-6d4c65a3ade0.png)

## After editing

![Editing MS Word Document in CSharp .NET](https://www.mindstick.com/mindstickarticle/019723e0-ec6c-4c95-a381-f40efba920f8/images/8ddcbfa2-c221-4d4a-8f7b-4a161140e591.png)

---

Original Source: https://www.mindstick.com/articles/58/editing-ms-word-document-in-csharp-dot-net

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
