Word document byte length?

D

Dean Slindee

Is there a way to determine a Word document's byte length? I need to know
this to ReDim a Byte() array.

Currently I am loading the document to a temp file and doing a
FileLen(document).

Would like to just get the byte length without the extra step of loading a
temp file.

Thanks,
Dean Slindee
 
C

Cindy M.

Hi Dean,
Is there a way to determine a Word document's byte length? I need to know
this to ReDim a Byte() array.

Currently I am loading the document to a temp file and doing a
FileLen(document).

Would like to just get the byte length without the extra step of loading a
temp file.
As far as I know, no, there's no other way to get this information, other
than to work with the file from disk (nothing in the Word object model). And
if the file is open in Word, then it needs to be done with a copy of the file
(due to file locking).

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 :)
 
T

Tony Jollans

I'll second what Cindy says - and I wouldn't like to guarantee that two
saves would necessarily produce two files of the same length. What exactly
do you want to do with the byte array and/or its contents?
 
D

Dean Slindee

My application saves a Word document (contract/appendices) into a SQL Server
table. I need to know the number of bytes comprising the Word document in
order to ReDim a Byte() array variable. That variable is part of the
structure that is passed to an Insert or Update command when saving the
contract into the datatable. A Byte() array must be preallocated large
enough to contain whatever is going to be loaded into it. Here is the code:
Dim lngFileSize As Long
'fill byte buffer
lngFileSize = FileLen(strContractPath)
Dim fsStream As FileStream
ReDim bytContractImage(CInt(lngFileSize - 1))
fsStream = New FileStream(strContractPath, FileMode.Open,
FileAccess.Read)
fsStream.Read(bytContractImage, 0, CInt(lngFileSize))
fsStream.Close()

My two attempts to get the length in Word were no where close to the file
size obtained above:
'try to load bytContractImage from tblVendorContract.ContractImage
lngFileSize = wrdApp.ActiveDocument.Characters.Count
Dim wrdSelection As Word.Selection
wrdDoc = wrdApp.ActiveDocument
wrdDoc.Select()
wrdSelection = wrdApp.Selection
lngFileSize = wrdApp.Selection.Range.Text.Length

No problem loading the same file size twice in a row with the top method.
I guess we'll leave it with the top method....

Thanks for the help.
Dean Slindee
 
T

Tony Jollans

Thank you for the further explanation.

You're loading a file from disk into your stream, and you must use the size
of that file. What Word holds in its internal memory is, potentially at
least, completely different from what is saved on disk and I suspect, even
Word does not know how big a document will be when saved until it saves it.
Certainly the character count comes nowhere near the answer you need - a
saved document also contains things such as document properties, style
definitions, etc., etc. I think you'll have to stick with what you have.
 

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