Introduction
This article contains description about how Word Document can be read and displayed in the screen using asp.net C#. reading Word document is very easy task , just have to follow some steps to get your task done. You have to follow only three steps are give bellow.
Steps
- Browse and read word document file from client machine
- Save it on the server
- Read file from server and display on the screen
Browse and Read Word
Document from client machine
To browse I did nothing, just I used the browse or file up loader control available in the ASP.NET server side controls list, the up loader controls in build asp.net controls helps to caching file information on the page .
Save it on the server and Read file from server and display on the screen
After browsing word document file
don't refresh your page by any of the control, because in the every refresh the
page loses his information and control information. Before the page's post back,
save the browsed word document in the server. To save and view the word
document I have take the help button click event and Microsoft.Office.Interop.Word dll.
- FileUpload1.SaveAs(Server.MapPath(FileUpload1.FileName));
- object filename = Server.MapPath(FileUpload1.FileName);
The server control FileUplod's saveas function helps to save file in the server and Server.MapPath function is used to map the path of the server.
- Microsoft.Office.Interop.Word.ApplicationClass AC = new Microsoft.Office.Interop.Word.ApplicationClass();
Above code create a object of application class which helps to open or create a word document. This class belongs Microsoft.Office.Interop name space.
Above code create the object of document class which keeps word content
- doc = AC.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);
- TextBox1.Text = doc.Content.Text;
above code opens the document that you have browsed set the content of the word document in to the textbox text.
HTML Code
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:TextBox ID="TextBox1" runat="server" Height="722px" Width="100%"
- TextMode="MultiLine"></asp:TextBox>
- </div>
- <div>
- <asp:FileUpload ID="FileUpload1" runat="server" BorderStyle="Double"
- Width="549px" /> <asp:Button ID="Button1" runat="server" Text="Button"
- onclick="Button1_Click" />
- </div>
- </form>
- </body>
- </html>
C# Code
- protected void Button1_Click(object sender, EventArgs e)
- {
- Microsoft.Office.Interop.Word.ApplicationClass AC = new Microsoft.Office.Interop.Word.ApplicationClass();
- Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document();
- object readOnly = false;
- object isVisible = true;
- object missing = System.Reflection.Missing.Value;
- try
- {
- doc = AC.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);
- TextBox1.Text = doc.Content.Text;
- }
- catch (Exception ex)
- {
- MessageBox.Show("ERROR: " + ex.Message);
- }
- finally
- {
- doc.Close(ref missing, ref missing, ref missing);
- }
- }
Summary
Above discussion we know how to read word document taking help of Microsoft.Office.Interop.Word
rerohit rerohitPosted Feb 4, 2020, 2:05 AM
Hi by this i can read only first page of word file now what to do to read all pages with image attached in wordtable
Sandip PatilPosted Aug 17, 2018, 1:27 AM
Hi kailash, i would like to Upload Resume and Read its Data like, name, Contact no, Email Address etc and Stored those filed into DB, how to Do it, please let me know or please send the email on [email protected], thanks and Regards Sandip Patil
srinivas prabhuPosted Oct 31, 2017, 1:19 AM
I want to display word document as it is
Former memberPosted Jun 13, 2015, 4:25 AM
install microsoft office in your server
Jisu SarkarPosted Jun 13, 2015, 3:28 AM
plese tell me how to fix it
Jisu SarkarPosted Jun 13, 2015, 3:28 AM
i have use your code but ApplicationClass is not working its showing the following errorError 3 'Microsoft.Office.Interop.Word.ApplicationClass' does not contain a definition for 'Documents' and no extension method 'Documents' accepting a first argument of type 'Microsoft.Office.Interop.Word.ApplicationClass' could be found (are you missing a using directive or an assembly reference?)Error 4 'Microsoft.Office.Interop.Word.ApplicationClass' does not contain a definition for 'Documents' and no extension method 'Documents' accepting a first argument of type 'Microsoft.Office.Interop.Word.ApplicationClass' could be found (are you missing a using directive or an assembly reference?) Error 5 Interop type 'Microsoft.Office.Interop.Word.ApplicationClass' cannot be embedded. Use the applicable interface instead. Error 6 Interop type 'Microsoft.Office.Interop.Word.ApplicationClass' cannot be embedded. Use the applicable interface instead.
ahmad sawalhaPosted Oct 11, 2014, 10:07 AM
I tired this method, but when I published the site to the server it gave an error. the Microsoft.Office.Interop can't be shared. So I have to user OpenXml instead, but it couldn't read doc files its for docx only, Any Idea?
adil khanPosted Jun 9, 2014, 9:45 AM
I want to check word exists or not in word file please help me
Surya VenkatesanPosted Jan 16, 2014, 9:58 AM
I tried this coding. but it shows an error "no overload for method open takes 12 arguments"
Surya VenkatesanPosted Jan 16, 2014, 9:57 AM
no overload for method open takes 12 arguments in asp.net
Priti KumariPosted Jan 9, 2014, 12:49 AM
i want to display formatted text in textbox.please help me.....thanks for your help
jamal jamalPosted Nov 27, 2013, 11:04 AM
Excellent This way, retrieve only text content, but how we can also display images that are inside word document ? Thanks for your help
hien ldPosted Nov 22, 2013, 9:51 PM
thank for share