Replace text in Word Document via ASP.NET

Posted by jreedinc on Stack Overflow See other posts from Stack Overflow or by jreedinc
Published on 2010-03-16T20:52:49Z Indexed on 2010/03/17 0:11 UTC
Read the original article Hit count: 675

Filed under:
|

How can I replace a string/word in a Word Document via ASP.NET? I just need to replace a couple words in the document, so I would like to stay AWAY from 3rd party plugins & interop. I would like to do this by opening the file and replacing the text.

The following attempts were made:

I created a StreamReader and Writer to read the file but I think that I am reading and writing in the wrong format. I think that Word Documents are stored in binary?? If word documents are binary, how would I read and write the file in binary?

    Dim template As String = Request.MapPath("documentName.doc")
    If File.Exists(template) Then
        Dim sr As New StreamReader(template)
        Dim content As String = sr.ReadToEnd()
        sr.Close()
        Dim sw As New StreamWriter(template)        
        content = content.Replace("@ T O D A Y S D A T E", Date.Now.ToString("MM/dd/yyyy"))
        sw.Write(content)
        sw.Close()
    Else

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about msword