Convert Document object to byte array without using hard drive

A

Adrian Johnson, CA

I'd like to use MS Word as a text editor and store the data as a byte[]
within a name in Excel. I'd like to embed a Word document in an Excel file
so the user can create rich 'how-to' documentation which gets transported
with the workbook.
Document's SaveAs and Open methods require a filename to be passed in, and a
Document object is not serializable.
To save the file, I could save the Document as a temporary file as an
intermediary, and then open up that file as a FileStream which I could then
convert to a byte array. However, creating the FileStream doesn't work if
the Document is still open in Word as it is being used by another process.
Outlook seems to be able to use Word as a text editor for composing email
messages, I'd like to do something similar.
Any ideas? I'm using C# in VS 2008 Professional, and Office 2003. Thanks.
 
C

Cindy M.

Hi =?Utf-8?B?QWRyaWFuIEpvaG5zb24sIENB?=,
I'd like to use MS Word as a text editor and store the data as a byte[]
within a name in Excel. I'd like to embed a Word document in an Excel file
so the user can create rich 'how-to' documentation which gets transported
with the workbook.
Document's SaveAs and Open methods require a filename to be passed in, and a
Document object is not serializable.
To save the file, I could save the Document as a temporary file as an
intermediary, and then open up that file as a FileStream which I could then
convert to a byte array. However, creating the FileStream doesn't work if
the Document is still open in Word as it is being used by another process.
Outlook seems to be able to use Word as a text editor for composing email
messages, I'd like to do something similar.
Any ideas? I'm using C# in VS 2008 Professional, and Office 2003.
About the only thing I can think of is to extract the information from the
document range using the XML property of the Range object into a String. You
can stream/serialize this with no problems.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
A

Adrian Johnson, CA

Thanks Cindy - that works fine, although obviously doesn't save any of the
document's other data (eg password protection etc). Note that the range
object in the Word 2003 document object model doesn't have an XML property
(2007 does). The get_XML method appears to do exactly the same thing, though
MS documentation says you're not meant to use it.

Cindy M. said:
Hi =?Utf-8?B?QWRyaWFuIEpvaG5zb24sIENB?=,
I'd like to use MS Word as a text editor and store the data as a byte[]
within a name in Excel. I'd like to embed a Word document in an Excel file
so the user can create rich 'how-to' documentation which gets transported
with the workbook.
Document's SaveAs and Open methods require a filename to be passed in, and a
Document object is not serializable.
To save the file, I could save the Document as a temporary file as an
intermediary, and then open up that file as a FileStream which I could then
convert to a byte array. However, creating the FileStream doesn't work if
the Document is still open in Word as it is being used by another process.
Outlook seems to be able to use Word as a text editor for composing email
messages, I'd like to do something similar.
Any ideas? I'm using C# in VS 2008 Professional, and Office 2003.
About the only thing I can think of is to extract the information from the
document range using the XML property of the Range object into a String. You
can stream/serialize this with no problems.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)


This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
C

Cindy M.

Hi Adrian,
Note that the range
object in the Word 2003 document object model doesn't have an XML property
(2007 does). The get_XML method appears to do exactly the same thing, though
MS documentation says you're not meant to use it.
Well, I guess that depends on which language you're using :) You've posted in a
VBA newsgroup, and in VBA you do see that property for the Range object... And
you certainly are meant to use it.

The reason C# has get_XML is because the property has an optional parameter
(which doesn't work as expected BTW - it's only when you're dealing with XML
Nodes from a schema you've attached, something the Help neglects to mention).
Since C# doesn't support parameters for properties, the PIA has to turn it into
a method. Don't know about what's going on with the 2007 PIA...

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
A

Adrian Johnson, CA

This is a VBA Newsgroup? Whoops! Sorry, I stumbled across it through
searching and saw it was called 'Word programming' so typed away. Thanks for
answering the question anyway though. And interesting to see that there are
properties available to VBA users that don't appear to be available to .NET
users.
 
C

Cindy M.

Hi Adrian,
This is a VBA Newsgroup? Whoops! Sorry, I stumbled across it through
searching and saw it was called 'Word programming' so typed away. Thanks for
answering the question anyway though. And interesting to see that there are
properties available to VBA users that don't appear to be available to .NET
users.
Right, the web interface :) You should see a second "Word.Programming" group in
that view. That one isn't specifically VBA, but regulars with .NET knowledge are
thin on the ground. Another possibility is the office.developer.automation
newsgroup; more .NET but fewer object library specialists.

FWIW, all the properties and methods are there that VBA uses, you simply can't
access all of them the same way when using .NET or C#. So when the PIA
"translates" the Word type library for use with .NET, it converts some of these
things to a form that C# can work with. Generally, if you know there should
be a property (you find a VB code example, for instance) and can't find it with
C# Intellisense, try looking for a "get_" version.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 

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