How To Read word document (like .doc, .rtf , txt ) in ASP.Net , C#






Here is the code that helps to read any document (like .doc, .rtf , txt ) from specified location. This is a web based application and this code is written in C# as code behind in ASP.Net 2.0, where the word document is hard to upload from client side. Here is the code that uploads the document file and stores it into a string and from that I have placed that string into a textbox. The First Step is that, we need to add a COM reference (that’s how we need to define the word application) to the project by right clicking in  the solution explorer on References->Add Reference. Click on the COM tab and look for the Microsoft Word 11.0 Object Library. Click Select
and OK.
Now u need to add the line <identity impersonate="true"/>
Like:
<system.web>
<identity impersonate="true"/>
in your web.config .
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ReadWordDocument.aspx.cs"
  Inherits="ReadWordDocument" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>Untitled Page</title>
</head>
<body>
  <form id="form1" runat="server">
      <div>
          <asp:FileUpload ID="FileUpload1" runat="server" /><br />
          <asp:Button ID="btnRead" runat="server" Text="Read Word Document" OnClick="btnRead_Click" /><br />
          <asp:TextBox ID="TextBox1" runat="server" Height="373px" TextMode="MultiLine" Width="500px"></asp:TextBox>
      </div>
  </form>
</body>
</html>


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.Office.Interop.Word;

public partial class ReadWordDocument : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {

  }
  protected void btnRead_Click(object sender, EventArgs e)
  {

      ApplicationClass wordApp = new ApplicationClass();

      // Input box is used to get the path of the file which has to be 
      // uploaded into textbox.

      string filePath = FileUpload1.PostedFile.FileName;

      object file = filePath;

      object nullobj = System.Reflection.Missing.Value;

      // here on Document.Open there should be 9 arg.
   
      Document doc = wordApp.Documents.Open(ref file,
                                            ref nullobj,
                                            ref nullobj,
                                            ref nullobj,
                                            ref nullobj,
                                            ref nullobj,
                                            ref nullobj,
                                            ref nullobj,
                                            ref nullobj,
                                            ref nullobj,
                                            ref nullobj,
                                            ref nullobj,
                                            ref nullobj,
                                            ref nullobj,
                                            ref nullobj,
                                            ref nullobj);

      // Here the word content is copeied into a string which helps to
      // store it into  textbox.

      Document doc1 = wordApp.ActiveDocument;

      string m_Content = doc1.Content.Text;

      // the content is stored into the textbox.

      TextBox1.Text = m_Content;

      doc.Close(ref nullobj, ref nullobj, ref nullobj);
  }
}

33 comments:

  1. this code is Really help ful

    ReplyDelete
  2. how to maintain view state of doc ???

    ReplyDelete
  3. this code is not working.

    ReplyDelete
  4. What is the error message you are getting ?

    ReplyDelete
  5. I'm using IIS server.

    ReplyDelete
  6. Do one thing,pass finename as hardcoded instead of selecting it from FileUpload control.like below

    object file = @"C:\1.doc";

    ReplyDelete
  7. I'm giving file like below
    string filePath = Server.MapPath("") + "\\ModelReports\\" + "xyz.rtf";

    object file = filePath;

    ReplyDelete
  8. For testing purpose only.Use hardcoded value and check.Code is working or not?

    string filePath = @"C:\xyz.rtf";

    ReplyDelete
  9. no,same problem

    ReplyDelete
  10. Are you using Microsoft.Office.Interop.Word or Microsoft Office Object Library...

    If using Microsoft.Office.Interop, you need to install it on server and point your reference dll.

    ReplyDelete
  11. i'm using Microsoft Office Object Library 12.0.

    ReplyDelete
  12. This code is working fine on my local machine.Can you test the code on local machine.

    ReplyDelete
  13. ya, this code is working fine on my local machine.

    ReplyDelete
  14. ohhhhhhh ...)

    I thought the problem is in the code.For this office should be installed on the production server.

    ReplyDelete
  15. yes, office is installed on server machine MS office 2003 and 2007

    ReplyDelete
  16. Have u entered this into web.config

    ReplyDelete
  17. <identity impersonate="true"/>

    ReplyDelete
  18. yes this tag have been added in web.config as u instructed.... i m using VS2010

    ReplyDelete
  19. let me clear which reference hv to add Microsoft.Office.Interop.Word or Microsoft Office Object Library... because using only Microsoft Office Object Library... not possible to use ur sample code...

    ReplyDelete
  20. Check event viewer on production server and send me the error details

    ReplyDelete
  21. I am using Microsoft Office Object Library

    ReplyDelete
  22. if i use only Microsoft Office Object Library 12.0 in VS 2008 then i didnt find ApplicationClass and other classes used in above sample code... & in namespace there is "using Microsoft.Office.Core;" instead of "using Microsoft.Office.Interop.Word;"

    ReplyDelete
  23. It's strange I am using Microsoft Office Object Library 12.0 .What is the error message in eventviewr?

    ReplyDelete
  24. As you told me that this code is working fine on local machine,but not working on production.

    So issue is on production,

    1.check the current logged in user have permission or not.

    2.Grant permission to asp_net user.

    ReplyDelete
  25. this code is only working on local system,not in server.
    i use this code for share point web part but
    "Document aDoc = WordApp.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, ref missing, ref missing, ref missing, ref missing);"
    return null.

    ReplyDelete
  26. @Alok:It's look like that there is some configuration mismatch between your local machine and server

    ReplyDelete
  27. really thank so much for prgm it helped me lot and works fine gud and once again thank u so much :)

    ReplyDelete
  28. Hi to everyone...
    I have use this code on local machine its work...but it is not working when i deploy my application on ftp sever... have you any idea about
    ?

    ReplyDelete
  29. hai i am using vs2012 and ms2010.i tried this code and not suceeded.iam getting error
    Error 1 Interop type 'Microsoft.Office.Interop.Word.ApplicationClass' cannot be embedded. Use the applicable interface instead. c:\users\sevak\documents\visual studio 2012\Projects\sampleviewquiz\sampleviewquiz\WebForm1.aspx.cs 21 13 sampleviewquiz
    Error 2 The type 'Microsoft.Office.Interop.Word.ApplicationClass' has no constructors defined c:\users\sevak\documents\visual studio 2012\Projects\sampleviewquiz\sampleviewquiz\WebForm1.aspx.cs 21 36 sampleviewquiz
    Error 3 Interop type 'Microsoft.Office.Interop.Word.ApplicationClass' cannot be embedded. Use the applicable interface instead. c:\users\sevak\documents\visual studio 2012\Projects\sampleviewquiz\sampleviewquiz\WebForm1.aspx.cs 21 40 sampleviewquiz
    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?) c:\users\sevak\documents\visual studio 2012\Projects\sampleviewquiz\sampleviewquiz\WebForm1.aspx.cs 25 30 sampleviewquiz
    Error 5 'Microsoft.Office.Interop.Word.ApplicationClass' does not contain a definition for 'ActiveDocument' and no extension method 'ActiveDocument' accepting a first argument of type 'Microsoft.Office.Interop.Word.ApplicationClass' could be found (are you missing a using directive or an assembly reference?) c:\users\sevak\documents\visual studio 2012\Projects\sampleviewquiz\sampleviewquiz\WebForm1.aspx.cs 40 31 sampleviewquiz

    ReplyDelete
  30. For the ApplicationClass error in Microsoft.office.interop.word. Go to the 12.0 reference and (F4) go to properties change 'Embed Interop types' as False.

    ReplyDelete
  31. Code not word in IIS7

    How to fix program?

    ReplyDelete
  32. There will be namespace error in Microsoft visual studio 2010.... so what can i do? plz suggest me solution of that problem

    ReplyDelete