Search This Blog

Tuesday 5 August 2014

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

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;

     
  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";
        }
    }
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";
    }
}

10.Browse the website and click on button to see result. 

No comments:

Post a Comment