Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to create and modify MS Word document in ASP.NET

Author: Mike Green
Download Source Code : 1348_CreateWordDoc.zip

I will explain that how you can create and modify MS Word document in ASP.NET.

 

ASP.NET provides the facility to create a word document in your application and later you can open, edit and modify already created word document. In .NET 4.0, you need to add the “Microsoft Word 12.0 Object Library” component reference in your application. This library has “Microsoft.Office.Interop.Word.dll” file which provides classes to create and modify word document. Moreover Microsoft Word has “Macros” to record and view macros. When you create a macro in MS Word, view and click on edit, it will show VBA code. This VBA code will give you an idea that how you can write your code to manipulate Word document.

First we will see how we can create a new Word document and then we will see how we can open already created Word document and insert text in it.

 

  1. Open MS Visual Studio 2010
  2. File > New > Website > Visual C# or Visual Basic > ASP.NET Empty Web Site
  3. Select Web Location as File System and Click OK
  4. From Menu, Website > Add New Item > Select Web Form and Click Add. Name of the Web Form will be Default.aspx
  5. Drag and Drop two Button controls and a Label Control or write code below in Default.aspx page

    <asp:Button ID="Button1" runat="server" Text="Create Word Document"
        onclick="Button1_Click" />
    <br />
    <br />
    <asp:Button ID="Button2" runat="server" Text="Open Existing and modify"
        onclick="Button2_Click" />
    <br />
    <br />
    <asp:Label ID="Label1" runat="server" ></asp:Label>
     
  6. From Menu, Website > Add Reference > Select COM tab > Select “Microsoft Word 12.0 Object Library” and Click OK
     
  7. Open code behind file and add following namespace in it

    C#
     
    using Microsoft.Office.Interop.Word;
    VB.NET
     
    Imports Microsoft.Office.Interop.Word
     
  8. Write code below in Button1 click event.

    C#
     
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            ApplicationClass wordApp = new ApplicationClass();
            object fileName = "D:\\TestDocument.docx";
            object missing = System.Reflection.Missing.Value;
            Document wordDoc = wordApp.Documents.Add(ref missing, ref missing, ref missing, ref missing);
     
            wordDoc.Activate();
            wordDoc.Content.InsertBefore("This is Word document created in ASP.NET");
            wordDoc.Content.InsertParagraphAfter();
            wordDoc.SaveAs(ref fileName);
            wordApp.Application.Quit(missing, missing, missing);
     
            Label1.Text = "Word document is created";
        }
        catch
        {
            Label1.Text = "Cannot create word document";
        }
    }
     
    VB.NET
     
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        Try
            Dim wordApp As New ApplicationClass()
            Dim fileName As Object = "D:\TestDocument.docx"
            Dim missing As Object = System.Reflection.Missing.Value
     
            Dim wordDoc As Document = wordApp.Documents.Add(missing, missing, missing, missing)
     
            wordDoc.Activate()
            wordDoc.Content.InsertBefore("This is Word document created in ASP.NET")
            wordDoc.Content.InsertParagraphAfter()
            wordDoc.SaveAs(fileName)
            wordApp.Application.Quit(missing, missing, missing)
     
            Label1.Text = "Word document is created"
        Catch
            Label1.Text = "Cannot create word document"
        End Try
    End Sub
     
    First we have created an object of ApplicationClass class. We have created objects for file name and missing object. You have to create an object of Document class and call the add() method of ApplicationClass class. Activate() method activates the specified object. InsertBefore() method will insert text at the start of the file.
     
  9. Write code below in Button2 click event.

    C#
     
    protected void Button2_Click(object sender, EventArgs e)
    {
        try
        {
            ApplicationClass wordApp = new ApplicationClass();
            object fileName = "D:\\TestDocument.docx";
            object missing = System.Reflection.Missing.Value;
            object visible = true;
            object readOnly = false;
     
            Document wordDoc = wordApp.Documents.Open(ref fileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, visible, ref missing, ref missing, ref missing, ref missing);
               
            wordDoc.Content.InsertAfter("Insert Text in already created word document");
            wordDoc.Save();
            wordApp.Application.Quit(missing, missing, missing);
            Label1.Text = "Text inserted in word document";
        }
        catch
        {
            Label1.Text = "Cannot insert text in word document";
        }
    }
     
    VB.NET
     
    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
        Try
            Dim wordApp As New ApplicationClass()
            Dim fileName As Object = "D:\TestDocument.docx"
            Dim missing As Object = System.Reflection.Missing.Value
            Dim visible As Object = True
            Dim [readOnly] As Object = False
     
            Dim wordDoc As Document = wordApp.Documents.Open(fileName, missing, [readOnly], missing, missing, missing, missing, missing, missing, missing, visible, missing, missing, missing, missing)
     
            wordDoc.Content.InsertAfter("Insert Text in already created word document")
            wordDoc.Save()
            wordApp.Application.Quit(missing, missing, missing)
            Label1.Text = "Text inserted in word document"
        Catch
            Label1.Text = "Cannot insert text in word document"
        End Try
    End Sub
     
    Here, we have created visible and readOnly objects because we need to provide these values in Open() method to open Word file.  InsertAfter() method will insert specified text at the end of the file.
     
  10. Browse the website and click on button to see result. 

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to bind multiple database fields to ListBox control in ASP.NET

Disclaimer - Privacy
© 2002-2017 DevASP.net