Create Microsoft Office Word Document in C#

 

We can create Microsoft Office Word document programmatically in C#. To create the Microsoft Office Word document we have to use Microsoft.Office.Interop.Word COM Library. In this article we discuss about how to create and how to write contents to Microsoft Office Word document by using COM library in C#.

 

Open Visual Studio and create some windows application. By right click on solution explorer select Add Reference => select COM tab => select "Microsoft Word 12.0 Object Library" as shown below.

 

 

 

 

Drag and Drop some button control to the Form1 win form. Import Microsoft.Office.Interop.Word namespace to the code and provide alias as MSWord. Then add below code to create and write contents to the Microsoft Office Word document.

 

using System; 

using System.Windows.Forms; 

using MSWord = Microsoft.Office.Interop.Word;

 

namespace CSharpCreateMSWordDocument 

{ 

    public partial class Form1 : Form 

    { 

        object objMiss = System.Reflection.Missing.Value;

 

        public Form1() 

        { 

            InitializeComponent(); 

        }

 

        public void AddContentToMSWord(MSWord.Document doc) 

        { 

            MSWord.Paragraph paragraph; 

            paragraph = doc.Content.Paragraphs.Add(ref objMiss);

 

            object styleHeading = "Heading 1"; 

            paragraph.Range.set_Style(ref styleHeading); 

            paragraph.Range.Text = "This is the Microsoft Word documnet created in C# With the help of COM library Microsoft.Office.Interop.Word"; 

            paragraph.Range.Font.Size = 10; 

            paragraph.Range.Font.Bold = 1; 

        }

 

        public void CreateMsWord() 

        { 

            try 

            { 

                MSWord.Application msWord = new MSWord.Application(); 

                MSWord.Document doc;

 

                msWord.Visible = true; 

                doc = msWord.Documents.Add(ref objMiss, ref objMiss, ref objMiss, ref objMiss);

                AddContentToMSWord(doc); 

            } 

            catch (Exception ex) 

            { 

                throw ex; 

            } 

        }

 

        private void button1_Click(object sender, EventArgs e) 

        { 

            CreateMsWord(); 

        } 

    } 

}

 

As shown above by using Application class in Microsoft.Office.Interop.Word namespace, we can create Microsoft Office application. By using Document class we can add word document to the Microsoft Office application. Add this document to the Microsoft Office application by using Documents property. 

Create some paragraph and add it to the word document by using Paragraphs property of word document. Add contents to the paragraph, you can mention font size, font weight....etc. 

Now run the application and click on the button, you can find some Microsoft Office Word will create and open with provided content.

Avail best discounts to purchase Microsoft Office 2013 package by using Microsoft Promo Code here

                                                                                    CSharpCreateMSWordDocument.zip (48.25 kb)