Replacing strings within a word doc and convert back to byte[]

  • Thread starter Christopher Beltran
  • Start date
C

Christopher Beltran

I am currently trying to replace certain strings, not
single characters, with other strings inside a word
document which is then sent to a browser as a binary
file. Right now, I read in the word file, convert the
FileStream into a string using Unicode encoding, then do a
replace, then convert the string back to a byte[] using
Unicode encoding which i then Response.WriteBinary(bytes)
to the browser. This works fine although the actual
replace is not working correctly. When I use UTF8 or
ASCII encoding the word document does not open correctly
in Word unfortunately. Anyone ever done this before? I
do not want to use the word component if possible or save
the word document back to disk if possible as its being
used as a template. Thank you so much!

Sincerely,
Christopher Beltran
VP Product Development, Passageways

P.O. Box 1950
West Lafayette, IN 47906

Telephone: (765) 497-8829
Direct Line: (765) 497-8831
Fax: (765) 497-8804
Email: (e-mail address removed)
 
B

Bret Mulvey

A Word file doesn't consist entirely of char's, so you can't load it into a
string without corrupting it. You might be successful loading it into a byte
array and doing the search and replace directly on the byte array, but you'd
have to come up with your own search and replace code because String.Replace
wouldn't be available.

I don't know the Word file format so there still may be problems with this
approach. For example, if Word keeps a checksum of the document to detect
corruption you'd have to update the checksum.
 
J

Joe Mayo

Christopher Beltran said:
I am currently trying to replace certain strings, not
single characters, with other strings inside a word
document which is then sent to a browser as a binary
file. Right now, I read in the word file, convert the
FileStream into a string using Unicode encoding, then do a
replace, then convert the string back to a byte[] using
Unicode encoding which i then Response.WriteBinary(bytes)
to the browser. This works fine although the actual
replace is not working correctly. When I use UTF8 or
ASCII encoding the word document does not open correctly
in Word unfortunately. Anyone ever done this before? I
do not want to use the word component if possible or save
the word document back to disk if possible as its being
used as a template. Thank you so much!

Hi Christopher,

The easiest way to work with Word docs is via COM Interop/VS.NET Tools for
Office or a 3rd party component, such as JiSys WordReports.

Here's the link to VS.NET Tools for Office:

http://msdn.microsoft.com/vstudio/office/default.aspx

Here's a link to JiSys:

http://www.jisys.com/

And a review:

http://www.csharp-station.com/Articles/WordReports.aspx

Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top