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
 

FREE 12 month online training for ASP.NET & MS Expression Studio and a Free copy of MS Expression Web with Windows Server Purchase
How to display word file content in ASP.NET Page

Author: Mike Green
Download Source Code : 1359_DisplayWordFileData.zip

I will explain how you can show word file content in ASP.NET page.

 

We can create, read and show word file content in our ASP.NET application using “Microsoft Word 12.0 Object Library” in .NET 4.0. We have to add of this library and include “Microsoft.Office.Interop.Word” namespace in our code file to create, modify and read word files in ASP.NET.

First we will create a word file, get data from an input field and save this data in word file. Then we will read this word file and display its content in another text field on the page.

 

  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. Write code below in Default.aspx page

    <textarea id="txtareaInput" rows="5" cols="30" runat="server"></textarea>
    <br />
    <br />
    <asp:Button ID="Button1" runat="server"
        Text="Insert Data in Word File and Show below" Height="25px" Width="250px"
        onclick="Button1_Click" />
    <br />
    <br />
    <textarea id="txtareaOutput" rows="5" cols="30" runat="server"></textarea>
     
  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 code behind file.

    C#
     
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            ApplicationClass appClass = new ApplicationClass();
            object fileName = "D:\\WordFile.docx";
            object missing = System.Reflection.Missing.Value;
     
            Document wordDoc = appClass.Documents.Add(ref missing, ref missing, ref missing, ref missing);
     
            wordDoc.Activate();
            string text = txtareaInput.InnerText;
            wordDoc.Content.InsertBefore(text);
            wordDoc.Content.InsertParagraphAfter();
            wordDoc.SaveAs(ref fileName);
            appClass.Application.Quit(ref missing, ref missing, ref missing);
     
            ReadWordFile(fileName);
        }
        catch(Exception ex)
        {
            Response.Write(ex.Message);
        }
    }
     
    private void ReadWordFile(object fileName)
    {
        try
        {
            ApplicationClass appClass = new ApplicationClass();
            object missing = System.Reflection.Missing.Value;
            object visible = true;
            object readOnly = false;
     
            Document wordDoc = appClass.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);
     
            txtareaOutput.InnerText = wordDoc.Content.Text;
     
            appClass.Application.Quit(ref missing, ref missing, ref missing);
        }
        catch(Exception ex)
        {
            Response.Write(ex.Message);
        }
    }
     
    VB.NET
     
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        Try
            Dim appClass As New ApplicationClass()
            Dim fileName As Object = "D:\WordFile.docx"
            Dim missing As Object = System.Reflection.Missing.Value
     
            Dim wordDoc As Document = appClass.Documents.Add(missing, missing, missing, missing)
     
            wordDoc.Activate()
            Dim text As String = txtareaInput.InnerText
            wordDoc.Content.InsertBefore(text)
            wordDoc.Content.InsertParagraphAfter()
            wordDoc.SaveAs(fileName)
            appClass.Application.Quit(missing, missing, missing)
     
            ReadWordFile(fileName)
        Catch ex As Exception
            Response.Write(ex.Message)
        End Try
    End Sub
     
    Private Sub ReadWordFile(ByVal fileName As Object)
        Try
            Dim appClass As New ApplicationClass()
            Dim missing As Object = System.Reflection.Missing.Value
            Dim visible As Object = True
            Dim [readOnly] As Object = False
     
            Dim wordDoc As Document = appClass.Documents.Open(fileName, missing, [readOnly], missing, missing, missing, missing, missing, missing, missing, visible, missing, missing, missing, missing)
     
            txtareaOutput.InnerText = wordDoc.Content.Text
     
            appClass.Application.Quit(missing, missing, missing)
        Catch ex As Exception
            Response.Write(ex.Message)
        End Try
    End Sub
     
    In button click event, first we have created ApplicationClass object and created an object for file name. Now we have to create Document class object and need to call the Add() method of ApplicationClass. We have called the activate() method which activates the specified object. we have retrieved text from input field and call the InsertBefore() method to insert this text at the start of the file. Now call the SaveAs() method to save file with specified name. Quit or close the word application. I have called the ReadWordFile() function in the button click event.
     
    In ReadWordFile() function I have created ApplicationClass object and Document object and called the Open() method to read file. Then I have assigned the text in word file to text field InnerText property.
     
  9. Browse the website, write text in input field and click on button to see result. 

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Website Navigation in ASP.NET

Disclaimer - Privacy
© 2002-2014 DevASP.net