Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Read Data From MS Word File (Document) in Asp.Net using C#, VB.Net

Jun 8, 2016
Introduction

Here I will explain how to read from ms word file in
asp.net using c#, vb.net with example or how to read data from word document in asp.net using c#, vb.net with example or how to read word document in asp.net using c#, vb.net with example. By using “Microsoft.Office.Interop.Word” reference in asp.net application we can read data from word file / document using c#, vb.net.


To read data from word document file in asp.net first create new word document file in your system and write some content in that to read like as shown below


Now in visual studio create new website and open your aspx page and write the following code


<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>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>
Read Word Document in ASP.Net using C#.Net & VB.Net</h2>
<table>
<tbody>
<tr>
<td>Select Word File
</td>
<td><asp:fileupload id="fileupload1" runat="server"></asp:fileupload></td>
</tr>
<tr>
<td></td>
<td><asp:button id="btnRead" onclick="btnRead_Click" runat="server" text="Read Document"></asp:button></td>
</tr>
<tr>
<td colspan="2">Content of Word Document:
</td>
</tr>
<tr>
<td colspan="2"><asp:textbox height="500px" id="txtread" runat="server" textmode="MultiLine" width="100%"></asp:textbox>
</td>
</tr>
</tbody></table>
</div>
</form>
</body>
</html>
After completion of adding aspx page code now we need to add “Micrsoft Office Object Library” reference to our application because to read word document we need to Microsoft word library reference to our application.

To add reference right click on your application à select add reference à Go to COM section à from that Microsoft Office Object Library à Click OK like as shown below.


Here you don’t need to worry about version of Microsoft Office Object Library because it will vary based on word installed on your computer.

After adding Microsoft word library reference now open code behind file and add following namespaces

C# Code


using System;
using System.Web;

After completion of adding namespaces you need to write the code like as shown below


protected void btnRead_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);
txtread.Text = msworddoc.Content.Text;
mswordappcls.Documents.Close();
}

VB.NET Code


Partial Class VBCode
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

End Sub
Protected Sub btnRead_Click(ByVal sender As Object, ByVal 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)
txtread.Text = msworddoc.Content.Text
mswordappcls.Documents.Close()
End Sub
End Class

Demo

Following is the demo of our application to read word document content in asp.net using c#vb.net.



If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

10 comments :

Unknown said...

Why to use com components? We can use OpenOffice XML instead. Com based components have lots of machine dependencies that can be faced while deploying this application to different server and same version of ms word software must be installed on that server.

Dhiru said...

C# code generating following error:
The type or namespace name 'ApplicationClass' does not exist in the namespace 'Microsoft.Office' (are you missing an assembly reference?)

Plz help

Unknown said...

Thanks sir

Anonymous said...

ApplicationClass does not exist. What is this ?? Please help

Unknown said...

Hi,
it is very good job but i need to know, How can we find data with any image from ms word?

Unknown said...

Error 1 The type or namespace name 'Interop' does not exist in the namespace 'Microsoft.Office' (are you missing an assembly reference?) E:\project\MJPEG\ConvertPage.aspx.cs 22 26 E:\project\MJPEG\

Unknown said...

This code is not give proper solution for me. so like something is different or wrong.

Darshana pandya said...

which version visual studio

Darshana pandya said...

which version visual studio

Anonymous said...

do you have any idea for opening a word document with full microsoft office control in windows form ?

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.