VBScript FileSystemObject/OpenTextFile ReadAll stops reading data at firs character zero

Posted on 2008-01-30
Medium Priority
Last Modified: 2013-12-20
I'm not sure of the terms, so please forgive me:  A supposedly text file is created by MS Access (the ObjectSaveAs function).  The file, when looked at with a hex tool, appears to be written in UniCode, instead of normal ASCII.  When I open the file (in Access) using the FilesSystemObject Object with the OpenTextFile command and try to do a ReadAll, the buffer has nothing but Character Zero's starting at the first Character Zero it finds.  Is this a known bug?  Can I still use the ReadAll function, only differently?  Is there an alternative native VBA command that can easily do the "ReadAll" function without having to code read loops, etc?

Code (without the obvious dimension lines):
            Set F = FSO.OpenTextFile(strTextFile, 1)
            strObjCode = F.ReadAll
            X = InStr(strObjCode, strSearch)

Sample snippet of Data (RSF.txt is the filename):
   RSF.TXT....|....1....|....2....|                                     This is a ruler line (not proportional)
              1 . V.e.r.s.i.o.n. .=.2.0.~                           This is the actual contents (The # 1 indicates First Byte)
                2200101010101010000000000              These two lines are the contents
                5580001010001010306050401              represented in hex (reading vertically)
Everthing from just after the "V" onward appears as character zeros in the strObjCode variable.

Question by:Faulkenator
  • 2
LVL 65

Accepted Solution

RobSampson earned 2000 total points
ID: 20780849
Try changing this:
Set F = FSO.OpenTextFile(strTextFile, 1)
to this
Set F = FSO.OpenTextFile(strTextFile, 1, False, -1)

There are four paramenters to the OpenTextFile method:

The third is whether to create a new file if it doesn't exist, and the last is open in UniCode or ASCII.



Author Comment

ID: 20781683
Neat!  Didn't see that in the help text.  I went back to it, and sure enough, there it is.  Thanks!   Time for a new pair of glasses!

FYI, as an alternative:  I used the Getfile & .Size parameter and used Read(Size) function.  That also worked, but it brings in all the in between zeros!

Thanks.  You get the nod.
LVL 65

Expert Comment

ID: 20781887
No problem, thanks.



Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

A quick solution showing how to control and open a POS Cash Register Drawer using VBA with MS Access.
What to do if a split doesn't fit? Or a bunch of invoice lines must be rounded while the sum must match a total? It takes a little, but - when done - it is extremely easy to implement.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

624 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