Read Microsoft Word Document File in Asp.Net using C#, Vb.Net

In this tutorial i am going to share with you how to read Microsoft Word document file in asp.net using c#, vb.net with example. To read data from word document file in asp.net first you need to prepare one word document file with some mining full content and save this file in your system.

Microsoft Word

After create one word file you should open your visual studio and create new website and add new webpage with extenson .aspx after than open your aspx file and write following code.

Now, you need to add “Microsoft Office Object Library” reference to your web application. Because to read Ms word document file you need to Microsoft word library reference to your web application.

To add reference you should right click on your web application >> Select add reference >> go to COM selection >> Select Microsoft Office Object Library >> Click OK.

Micrsoft Office Object Library

After add “Microsoft Office Object Library” reference you should open your .aspx.cs file (code behind file) and add following namespaces.
using System.Web;
aspx code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>How To Read MS Word File in Asp.Net using C#, VB.Net</title>
    <style type="text/css">
        .auto-style1 {
            text-align: center;
        }

        .auto-style2 {
            width: 217px;
        }

        .auto-style3 {
            text-align: center;
            color: #FFFFFF;
            height: 31px;
            background-color: #FF9900;
        }

        .auto-style4 {
            color: #FFFFFF;
            height: 27px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <h2 class="auto-style1">Read Word Document in ASP.Net using C#.Net & VB.Net</h2>
            <table align="center" style="width: 356px">
                <tbody>
                    <tr>
                        <td>Select Word File
                        </td>
                        <td class="auto-style2">
                            <asp:FileUpload ID="fileupload1" runat="server" Height="39px" Width="223px"></asp:FileUpload></td>
                    </tr>
                    <tr>
                        <td>&nbsp;</td>
                        <td class="auto-style2">&nbsp;</td>
                    </tr>
                    <tr>
                        <td></td>
                        <td class="auto-style2">
                            <asp:Button ID="btnReaddata" runat="server" Text="Read Document" BackColor="#009900" BorderStyle="None" ForeColor="White" Height="39px" Width="145px" OnClick="btnReaddata_Click"></asp:Button>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">&nbsp;</td>
                    </tr>
                    <tr>
                        <td colspan="2" class="auto-style3">Content of Word Document:
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <asp:TextBox Height="211px" ID="txtreaddata" runat="server" TextMode="MultiLine" Width="100%"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2" class="auto-style4" style="text-align: center; background-color: #FF9900">Developed By: Nikunj Satasiya</td>
                    </tr>
                </tbody>
            </table>
        </div>
    </form>
</body>
</html>

After adding namespaces you need to write the following code.
C# Code:
protected void btnReaddata_Click(object sender, EventArgs e)
    {
        object filename = Server.MapPath(fileupload1.FileName);
        Microsoft.Office.Interop.ApplicationClass mswordappcls = new Microsoft.Office.Interop.Word.ApplicationClass();
        Microsoft.Office.Interop.Word.Document msworddoc = new Microsoft.Office.Interop.Word.Document();
        object readOnly = false;
        object isVisible = true;
        object missing = System.Reflection.Missing.Value;
        msworddoc = mswordappcls.Documents.Open(ref filename, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible);
        txtreaddata.Text = msworddoc.Content.Text;
        mswordappcls.Documents.Close();
    }
Vb.Net Code:
Imports System.Web
Imports System
Partial Class Default2
    Inherits System.Web.UI.Page
    Protected Sub btnReaddata_Click(ByVal sender As ObjectByVal e As EventArgs)
        Dim filename As Object = Server.MapPath(fileupload1.FileName)
        Dim mswordappcls As Microsoft.Office.Interop.ApplicationClass = New Microsoft.Office.Interop.Word.ApplicationClass()
        Dim msworddoc As New Microsoft.Office.Interop.Word.Document()
        Dim [readOnly] As Object = False
        Dim isVisible As Object = True
        Dim missing As Object = System.Reflection.Missing.Value
        msworddoc = mswordappcls.Documents.Open(filename, missing, [readOnly], missing, missing, missing, missing, missing, missing, missing, missing, isVisible)
        txtreaddata.Text = msworddoc.Content.Text
        mswordappcls.Documents.Close()
    End Sub
End Class
Demo:

Previous
Next Post »

If you have any kind of question about any post, Feel free to ask.You can simply drop a comment below post. Your feedback and suggestions will be highly appreciated. ConversionConversion EmoticonEmoticon