Unexpected Error when Dereferencing a Byte Array in VBScript

Billy Gayts 0 Reputation points
2026-07-28T20:12:15.5733333+00:00

So, I am writing a VBS script that at some point needs to read a file byte by byte and do some computations on each byte. So, I use the following code to read each byte:

Set Stream = CreateObject("ADODB.Stream")
Stream.Type = 1   ' aka. adTypeBinary
Stream.Open
Stream.LoadFromFile SomePath
Do while Stream.Position < Stream.Size
    buf = Stream.Read(1)
	dostuff buf(0)                         ' TYPE MISMATCH ERROR, "buf"
Loop

The problem is, that a type mismatch error occurs when I dereference the 0th element of the byte array. And what really bothers me is that if I do msgbox IsArray(buf) or msgbox LBound(buf) they print True and 0 respectively, and msgbox TypeName(buf) prints Byte(). So I know it is a Byte array and I know there is a 0th element. And I know it is not the sub I call because if I do tmp = buf(0) I get the same exact error.

My question: Why is this error occuring, is it a VBScript Bug and how do I work around it?

Developer technologies | Visual Basic for Applications

Answer accepted by question author

Marcin Policht 98,145 Reputation points MVP Volunteer Moderator
2026-07-28T21:28:52.14+00:00

ADODB.Stream.Read(1) returns a Variant containing a SAFEARRAY of bytes, not a native VBScript array. Although IsArray(), TypeName(), and LBound() correctly identify it as a byte array, VBScript cannot directly index certain COM-returned byte arrays, resulting in a Type mismatch when you access buf(0).

This is a limitation of VBScript's interaction with COM automation objects rather than a bug in ADODB.Stream itself. VBScript handles normal arrays and COM SAFEARRAYs differently, and byte arrays are one of the edge cases.

As a workaround, try using AscB() on a one-byte binary string instead of indexing the byte array, or to read larger blocks and process them through another COM object (such as Scripting.FileSystemObject or a custom COM component). If you only need one byte at a time, another option is to use MidB()/AscB() on binary data after converting it appropriately.

If you must use ADODB.Stream, reading larger chunks and processing them outside of direct buf(0) indexing is generally the most reliable approach.


If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.

hth

Marcin

Was this answer helpful?

2 people found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Taki Ly (WICLOUD CORPORATION) 2,980 Reputation points Microsoft External Staff Moderator
    2026-07-29T04:25:21.9566667+00:00

    Hello @Billy Gayts ,

    I have tested the approach suggested by Marcin. By utilizing AscB(MidB(buf, 1, 1)) instead of directly indexing the array with buf(0), it is possible to retrieve the byte value without triggering the Type mismatch error. This suggests that using native VBScript binary string functions is a reliable approach for handling COM-returned byte arrays in this scenario.

    For your reference, it might be beneficial to review the following documentation regarding ADO Stream and VBScript data handling:

    If the suggested approach works for you as well, a quick confirmation would be greatly appreciated.

    Otherwise, please let me know if you have any further questions or if the issue persists. If I do not hear back, I will proceed to close this case in the next few days.

    Thank you.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.