Monday, May 22, 2017

Upload and read word document in

In this article I am going to explain how to upload and read word document in

I want to upload and read the word document. Word document will be save to folder and content on doc will be displayed in textbox.


HTML markup:
<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
            <tr><td>Upload File :</td><td><asp:FileUpload ID="FileUpload1" runat="server" /></td></tr>
             <tr><td></td><td><asp:Button ID="Button1" runat="server" Text="Button" /></td></tr>
             <tr><td colspan="2"></td></tr>
             <tr><td colspan="2"><asp:TextBox ID="txtarea" runat="server" TextMode="MultiLine" Width="600px" Height="500px" Visible="false"></asp:TextBox></td></tr>

Add namespace
C# Code:
using System.IO;
using Microsoft.Office.Interop.Word; Code:
Imports System.IO
Imports Microsoft.Office.Interop.Word

On button click write the below given code
C# Code:
protected void Button1_Click(object sender, EventArgs e)
            string filepath = Server.MapPath("~/img") + Path.GetFileName(FileUpload1.FileName);
            Application worddoc = new Microsoft.Office.Interop.Word.Application();
            object miss = System.Reflection.Missing.Value;
            object path = filepath;
            object readOnly = true;
            Document docs = worddoc.Documents.Open(ref path, ref miss, ref readOnly, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
            txtarea.Visible = true;
            txtarea.Text = docs.Content.Text;
        catch (Exception ex) { }
    } Code:
    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim filepath As String = Server.MapPath("~/img") + Path.GetFileName(FileUpload1.FileName)
            Dim worddoc As Application = New Microsoft.Office.Interop.Word.Application()
            Dim miss As Object = System.Reflection.Missing.Value
            Dim path__1 As Object = filepath
            Dim [readOnly] As Object = True
            Dim docs As Document = worddoc.Documents.Open(path__1, miss, [readOnly], miss, miss, miss, _
                miss, miss, miss, miss, miss, miss, _
                miss, miss, miss, miss)
            txtarea.Visible = True
            txtarea.Text = docs.Content.Text
        Catch ex As Exception
        End Try
    End Sub

No comments:

Post a Comment