Article
    C#
    ADO.Net
    .NET
    ASP.Net & Web Forms
    Custom Controls
    Web Development
    Exception Handling
    XML
    Database
    Security in .Net
    Testing
    Web Services
    Windows Services
    Windows Controls
    WCF
    AJAX
    WPF
    XAML
    Reporting
    Setup
    VB.Net
    LINQ
    JQuery
    SilverLight
    JavaScript
    HTML5
    Crystal Report
    Cloud Computing
    Share Point
    Visual C++
    MVC
    Android
    PHP
    Java
    HTML
    WordPress
    Joomla
    Products
    Drupal
    Windows Phone
    JSON
    LightSwitch
    iPhone/iPad
    Ruby on Rails
    IIS 7
    Windows 8
    CSS/CSS3
    Excel
    MS Access
    Shortcut Keys
    Visual SourceSafe
    Team Foundation Server
    API(s)
    Sencha-Touch
    Single Page App
    Bootstrap
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Top Contributor
Advertisement
Advertise with Us
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog

Home >> C# >> Create Microsoft Word Document by using C#
Create Microsoft Word Document by using C#
Create Microsoft Word Document by using C#


by Arun Singh on 8/25/2011 6:25:03 PM

Views: 23843       Comments: 1

Create Microsoft Word Document by using C#

To create Microsoft word document by using C#.net follow the following steps.

Step 1: First open visual studio and create new window form application project by opening file menu and click new project then select project.

Step 2: After successful completion of creating new project write click on the project name and select Add References... in Solution Explorer which appear in left pane of visual studio.

After Click on Add reference then following window will be appear and select Microsoft Word 12.0 Object Library dll and click ok.

Step 3: After the Successfully added dll in your project write the following code to create MS-Word Document.

Here I am making a window form which perform two task first one is create ms-word document and second one is read document. 

Step 4: Now debug the code and click on the create button after clicking create button the create button click event generate

public partial class Form1 : Form

    {

        // create MS-Word application 

        Microsoft.Office.Interop.Word.Application msWord = new Microsoft.Office.Interop.Word.Application();

        // create Word document reference

        Microsoft.Office.Interop.Word.Document doc;

        // Create misssing value object

        object objMiss = System.Reflection.Missing.Value;

        // Create end of document object

        object endofdoc = "\\endofdoc";

        public Form1()

        {

            InitializeComponent();

        }

        /// <summary>

        /// This Method create first paragraph 

        /// </summary>

        public void FirstPara()

        {

            // create first paragraph with reference name

            Microsoft.Office.Interop.Word.Paragraph para1;

            // add paragraph with document

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

            // create object of heading style

            object styleHeading1 = "Heading 1";

            //add heading style with paragraph

            para1.Range.set_Style(ref styleHeading1);

            // Write text of paragraph

            para1.Range.Text = "Hello Arun, How are You?";

            //set font style of paragraph

            para1.Range.Font.Bold = 1;

            // set space after write format of paragraph

            para1.Format.SpaceAfter = 24;

            // selection range of after insert paragraph

            para1.Range.InsertParagraphAfter();

        }

        /// <summary>

        /// This Method Create Second Paragraph

        /// </summary>

        public void SecondPara()

        {

            // create second paragaraph  with paragraph reference name para2

            Microsoft.Office.Interop.Word.Paragraph para2;

 

            // add second paragraph with documnet

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

            // set paragraph heading style

            object styleHeading2 = "Heading 2";

            // add heading style with paragraph

            para2.Range.set_Style(ref styleHeading2);

            // second paragraph text 

            para2.Range.Text = "Hii This is Arun I am fine and you?";

            // set second paragraph font style

            para2.Range.Font.Bold = 1;

            // space or font size style like 24pt, 25pt etc.

            para2.Format.SpaceAfter = 24;

            // set selection range of paragraph

            para2.Range.InsertParagraphAfter();

        }

        /// <summary>

        /// This Method create table in ms-word document

        /// </summary>

        public void CreateTable(int Row, int column)

        {

 

            // create table in word documnet in word application with table reference name tbl1

            Microsoft.Office.Interop.Word.Table tbl1;

            // calculate the range of endofdocu

            Microsoft.Office.Interop.Word.Range wordRange = doc.Bookmarks.get_Item(ref endofdoc).Range;

            // add table with document with number of row and column

            tbl1 = doc.Content.Tables.Add(wordRange, 3, 3, ref objMiss, ref objMiss);

            // set border visibility true by input 1 and false by input 0

            tbl1.Borders.Enable = 1;

            // set text in each cell of table

            for (int r = 1; r <= 3; r++)

            {

                for (int c = 1; c <= 3; c++)

                {

                    tbl1.Cell(r, c).Range.Text = "r" + r + "c" + c;

                }

            }

        }

 

/// <summary>

///This method creates ms-word document and adding some paragraph, table and much more.

/// </summary>

 

        public void CreateMsWord()

        {

            try

            {

               // show ms-word application

                msWord.Visible = true;

                // add blank documnet in word application

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

                // create first para

                FirstPara();

                // create Second para

                SecondPara();

                // create table

                CreateTable(3, 3);

 

            }

            catch (Exception ex) { MessageBox.Show(ex.ToString()); }

          

        }

 

        /// <summary>

        /// create button click event 

        /// </summary>

        private void btn_Create_Click(object sender, EventArgs e)

        {

            // call the method to create ms word document file

            CreateMsWord();

        }

Desired Output:

When you click on Create button then following ms-word document file created.

Report Abuse Form
Reason:    
 


Easily create Word files in C#
by James Howard 2/15/2012 7:29:19 AM
Hi,

you can also easily C# create Word files with this C# / VB.NET Word library.

Here is a sample code:
// Create a new empty document.
DocumentModel document = new DocumentModel();

// Add document content.
document.Sections.Add(
    new Section(document,
        new Paragraph(document, "Hello World!")));

// Save the document to a file.
document.Save("Document.docx", SaveOptions.DocxDefault);
Report Abuse
Title :
Comment :
Text ColorBackground Color
BoldItalicUnderline
LeftCenterRightJustify
Ordered ListBulleted List
IndentOutdent
Horizontal Rule
SubscriptSuperscript
HyperlinkImage
Design ModeDesign
View HtmlHtml
     
 
Latest Article by Arun SinghRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Latest BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Top Viewed ArticlesRSS Feed
    
    
    
    
    
    
    
    
    
    
Top Viewed BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
Latest Interview QuestionsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Total Online Users: 4663
Advertisement
MindStick SurveyManager
Advertise with Us
  
Copyright © 2009 - 2014MindStick. All Rights Reserved.