Create and Write Contents to Microsoft Office Word Document in C#

 

We can create, Write, Open Microsoft Office Word document using C# dynamically. In this article we discuss about how to create Microsoft Office Word document in C#.  We have to use Microsoft.Office.Interop.Word dll to create Microsoft Word document.

 

Create new .Net windows application => add Microsoft.Office.Interop.Word.dll by selecting the Add Reference option by right click on solution explorer and select Microsoft.Office.Interop.Word.dll as shown below.

 

                   

 

Now create the Microsoft Office Word document by using _Application interface under Microsoft.Office.Interop.Word namespace as shown below.

 

using System;

using System.Diagnostics;

using System.Reflection;

using System.Windows.Forms;

using Microsoft.Office.Interop.Word;

 

namespace CSharpCreateMSOfficeWord

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        private void btnMSWord_Click(object sender, EventArgs e)

        {

            CreateMicrosoftWord();

        }

 

        private void CreateMicrosoftWord()

        {

            object oMissing = Missing.Value;

            object oVisible = true;

            object oStart = 0;

 

            _Application oWordApp = new Microsoft.Office.Interop.Word.Application();

            Document oMSDoc = oWordApp.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);

            Range oRange = oMSDoc.Range(ref oStart, ref oMissing);

 

            //path for the sample Microsoft Office Word document

            object oFile = System.Windows.Forms.Application.StartupPath + "\\MicrosoftOfficeWord.docx";

            try

            {

                oRange.Font.Name = "Verdana";

                oRange.Font.Size = 20;

                oRange.Font.Bold = 1;

                oRange.Font.Italic = 1;

                oRange.Font.Underline = WdUnderline.wdUnderlineSingle;

 

                oRange.InsertAfter("This is an Example for how to create Microsoft Office Word Document in C#");

                oMSDoc.SaveAs(ref oFile, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,

                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

 

                oMSDoc.Close(ref oMissing, ref oMissing, ref oMissing);

                Process.Start(oFile.ToString());

            }

            catch (Exception ex)

            {

                throw ex;

            }

        }

    }

}

 

As shown above we are creating the Microsoft Word document by using Add() method of _Application Documents property. We have to use Range class to specify the Font name, size, Bold, Italic and Underline for text.

 

We are using the InsertAfter() method of Range class to write the content to the Microsoft Word document. Use Start() method of Process class to open Microsoft Word document in C#.

 

By clicking on "Create & Open Microsoft Word" button, the output is as shown below.

 

                     

                                                                                                                                                                          

                                                                                                     CSharpCreateMSOfficeWord.zip (320.89 kb)