By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,955 Members | 1,768 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,955 IT Pros & Developers. It's quick & easy.

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

P: n/a
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: ch***@passageways.net
Nov 15 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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.
"Christopher Beltran" <ch***@passageways.net> wrote in message
news:05****************************@phx.gbl...
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: ch***@passageways.net

Nov 15 '05 #2

P: n/a

"Christopher Beltran" <ch***@passageways.net> wrote in message
news:05****************************@phx.gbl...
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
--
http://www.csharp-station.com
Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.