I'm running into a strange block, and I need some help. I want ot store .doc files in a database as blob fields, and I have ADO figured out sufficiently to do this. My problem is in reading the file initially from the hard disk, and writing it later after I retrieve it again from the database.
Using Open <filename> for binary as #1, I never have any problems; I can read and write a Word 2000 document with no trouble as a byte array, preserving integrity as I go. I also save the space in the database by writing an ascii style byte array, rather than a unicode twobyte system.
However...
I would really like to write this as a VBScript file, for ease of modification purposes. Unfortunately, VBScript does not support the Open statement, so I tried to use the Scripting.FileSystemObject. THis seems to read files fine, whether as ascii or unicode, into byte arrays. The only way I can use the FileSystemObject to write, however, is to use Strconv to push the ascii blob back into a unicode format that the FileSystemObject can write properly. If I don't, it writes garbage. My biggest problem is that Microsoft has eliminated StrConv from it's list of supported functions for VBScript!!
I have tried writing the full unicode byte array to the database, and I have no trouble, other than that it takes up twice as much room. I really want ot be able to send it as ascii.
Try this, it is in VB using Scripting Runtime but should easily convert to VB script.
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fso As FileSystemObject, f As File
Dim tsWord As TextStream
Dim tsRead As TextStream
Dim aryWord() As Byte
Private Sub Command1_Click()
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CreateTextFile "c:\testbed\testnew.doc", True, False
Set f = fso.GetFile("c:\testbed\testnew.doc")
Set tsWord = f.OpenAsTextStream(ForWriting, TristateFalse)
Set tsRead = fso.OpenTextFile("c:\testbed\test.doc", ForReading, True, TristateFalse)
Do
tsWord.Write tsRead.Read(1)
Loop Until tsRead.AtEndOfStream
tsWord.Close
tsRead.Close
Set f = Nothing
Set fso = Nothing
End Sub
I have tried it with some word documents and it creates a perfectly openable copy of the document.
I obviously haven't saved the textstream to a database in the meantime but as you have that figured you should be able to use this code to solve your problem.
Some notes about Unicode...
Each symbol presented as two bytes. For common fonts first byte is 0 and second is ascii code of symbol. So for common fonts you can just skip zeros.
The problem starts when you receive 'really' unicode symbol... Convert two bytes to long, and if value is less than zero you just need to add 4096 to get appropriate ascii symbol.
So, final solution (or your function StrConv) could be following:
A = B1*256 + B2
If A<0 then
A=A + 4096
end if
Now A consist of ascii code of symbol.
Sincerely yours,
Crin
PS. English is not my native language, so please ask for more explanations if needed.
Nick, you can set the number of characters read in each call to a higher figure <= 999999999 in fact so that you don't have to do it char by char. I have also tried it by reading the entire file into the aryWord array and then writing the contents back and it seems to work fine!
Private Sub Command1_Click()
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CreateTextFile "c:\testbed\testnew.doc", True, False
Set f = fso.GetFile("c:\testbed\testnew.doc")
Set tsWord = f.OpenAsTextStream(ForWriting, TristateFalse)
Set tsRead = fso.OpenTextFile("c:\testbed\test.doc", ForReading, True, TristateFalse)
Do
aryWord = tsRead.Read(9999999)
tsWord.Write aryWord
Loop Until tsRead.AtEndOfStream
tsWord.Close
tsRead.Close
Set f = Nothing
Set fso = Nothing
End Sub
0
Your question, your audience. Choose who sees your identity—and your question—with question security.
I thank you all for your comments, but I think I may not have clearly explained the point. I know that by using TriStateFalse, I can get a solution that works, but the resulting byte array is twice as massive as if I were able to open the file with "Open <filename> for Binary as #number". Thus, the blob inputted into the database takes up more room. If I could use strconv in vbscript, it would be no problem.
Try this...open your documents using TriStateTrue, instead of TriStateFalse, or use the open statement. YOu won't be able to write back the string to the file using the filesystemobject successfully, I believe.
I want to post a code for custom StrConv function (let us name it mStrConv) but I need a little example from VB that worked before using MS StrConv function.
Could you show it?
My previous message just provided the way you can create such function in VBScript.
THanks for the guidance to web pages. Unfortunately, I haven't been able to find reference to anything concerning the reasons for dropping support for strconv in vbscript, nor what the alternatives are. Thanks.
Crin : It is a bit difficult to post here. If you give me an email address, I can send something to you.
I've solved the problem in a different way...use java. Unfortunately, it just looked like the FileSystemObject was imperfect in its handling of ascii streams...if you come up with a manual way of converting ascii to unicode, drop me a line:
niheitz@hotmail.com
0
Question has a verified solution.
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fso As FileSystemObject, f As File
Dim tsWord As TextStream
Dim tsRead As TextStream
Dim aryWord() As Byte
Private Sub Command1_Click()
Set fso = CreateObject("Scripting.Fi
fso.CreateTextFile "c:\testbed\testnew.doc", True, False
Set f = fso.GetFile("c:\testbed\te
Set tsWord = f.OpenAsTextStream(ForWrit
Set tsRead = fso.OpenTextFile("c:\testb
Do
tsWord.Write tsRead.Read(1)
Loop Until tsRead.AtEndOfStream
tsWord.Close
tsRead.Close
Set f = Nothing
Set fso = Nothing
End Sub
I have tried it with some word documents and it creates a perfectly openable copy of the document.
I obviously haven't saved the textstream to a database in the meantime but as you have that figured you should be able to use this code to solve your problem.