How can I open MS Word document using ASP.NET

John Wen

Member
Joined
Jul 3, 2008
Messages
8
Programming Experience
1-3
Hello

First of all, I can open the MS Word document using VB.NET. However, I cannot do the same in ASP.NET (error message is "Access is denied") using the same method of VB.NET. What is wrong? Perhaps it might be the problem of permission, but I have already added the ASP.NET User account for the Word.Application from the registry.

How can I open MS Word document using ASP.NET?

Best regards
John Wen
17 - 7 - 08
 
Translation of the above post. Seems to work fine.

VB.NET:
        Try
            Dim stream As FileStream = File.Open("d:\word.doc", FileMode.Open) 'replace the location with your word doc.

            stream.Position = 0

            ' Now read s into a byte buffer.
            Dim bytes() As Byte = New Byte(stream.Length) {}
            Dim numBytesToRead As Integer = CType(stream.Length, Integer)
            Dim numBytesRead As Integer = 0
            While numBytesToRead > 0
                ' Read may return anything from 0 to numBytesToRead.
                Dim n As Integer = stream.Read(bytes, numBytesRead, numBytesToRead)
                ' The end of the file is reached.
                If n = 0 Then
                    Exit While
                End If
                numBytesRead += n
                numBytesToRead -= n
            End While
            stream.Close()

            Response.Clear()
            Response.ContentType = "application/word"
            Response.AddHeader("Content-disposition", "filename=output.doc")

            Response.OutputStream.Write(bytes, 0, bytes.Length)
            Response.OutputStream.Flush()
            Response.OutputStream.Close()
            Response.Flush()
            Response.Close()
        Catch ex As Exception
            Throw ex
        End Try
 
Hello

See the following codes, then why "Access is denied"? I do want to know the reason of "Access is denied". It is because the same code can apply to VB.NET. Please help.

Regards
John Wen
VB.NET:
  Dim oWord As New Word.Application
        Dim oDoc As Word.Document

        oDoc = oWord.Documents.Open("c:\rubbish\resume.doc")
        Response.Write("<font color=blue>" & oDoc.Content.Text & "</font>")
 
Hi John

I don't know how you add ASPNet account to Word. As web-based application is running under a process identity, I think a better way is to impersonate your ASP.Net application's process identity to use a higher privilege account. This can be done in Web.config.

For more idea about process identity, please visit my blog (though not 100% fit your question) at http://www.biswaretech.com/blog15.aspx
 
Back
Top