Solved

VBScript String Conversion

Posted on 2000-05-11
11
3,095 Views
Last Modified: 2010-05-18
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.

Can anyone suggest a solution?  
0
Comment
Question by:NickHeitz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 2800171
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.
0
 
LVL 2

Expert Comment

by:Crin
ID: 2800209
Hello,

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.
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 2800447
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:NickHeitz
ID: 2803956
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.
0
 
LVL 2

Expert Comment

by:Crin
ID: 2804177
Hello Nick,

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.

Sincerely yours,

Crin


0
 
LVL 2

Expert Comment

by:orbitaltech
ID: 2815019
NickHeitx,
Try this site and see if it will help you. I stink at VBscript but anytime I need vbscript help, I get it from here:

http://www.vb-web-directory.com/vbscript/vbscript.shtml

-good luck


0
 
LVL 2

Expert Comment

by:orbitaltech
ID: 2815021
0
 

Author Comment

by:NickHeitz
ID: 2816400
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.
0
 
LVL 2

Accepted Solution

by:
Crin earned 400 total points
ID: 2816779
crin@mail.com

Sincerely yours,

Crin
0
 

Author Comment

by:NickHeitz
ID: 2964033
Comment accepted as answer
0
 

Author Comment

by:NickHeitz
ID: 2964034
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question