I have a piece of code that I need to basically put all contents of a huge buffer output to a file. So far I am able to get it working with a smaller buffer, but if I try to use a really large buffer, I get too many line continuation errors, which means I need to break it up? Is there any way I can have multiple Arrays possibly and join them to 1 file, perhaps through concatenation? I know the concept of using strings and byte Arrays but in this case, Im only able to use my Array as a Variant, not as Byte. I'm new to VBA scripting so I'd to learn from this example. Thanks.
Sub WriteBinary(FileName, buf)
Dim I, aBuf, Size, bStream
Size = UBound(buf): ReDim aBuf(Size \ 2)
For I = 0 To Size - 1 Step 2
aBuf(I \ 2) = ChrW(buf(I + 1) * 256 + buf(I))
Next
If I = Size Then aBuf(I \ 2) = ChrW(buf(I))
aBuf = Join(aBuf, "")
Set bStream = CreateObject("ADODB.Stream")
bStream.Type = 1: bStream.Open
With CreateObject("ADODB.Stream")
.Type = 2: .Open: .WriteText aBuf
.Position = 2: .CopyTo bStream: .Close
End With
bStream.SaveToFile FileName, 2: bStream.Close
Set bStream = Nothing
End Sub
Sub Test()
Dim buf As Variant
buf = Array(184, 97, 61, 32, 0, 4, ....)
WriteBinary "C:\file.txt", buf
End Sub
My new code edited is below. I was able to get an array out of strings I suppose utilizing the net framework ArrayList but I am still having trouble with WriteBinary using it. I know that there is a difference between an Array(byte code) and Array "byte code". I just dont know how to make the latter work with my function.
Sub WriteBinary(FileName, buf)
Dim I, aBuf, Size, bStream
Size = UBound(buf): ReDim aBuf(Size \ 2)
For I = 0 To Size - 1 Step 2
aBuf(I \ 2) = ChrW(buf(I + 1) * 256 + buf(I))
Next
If I = Size Then aBuf(I \ 2) = ChrW(buf(I))
aBuf = Join(aBuf, "")
Set bStream = CreateObject("ADODB.Stream")
bStream.Type = 1: bStream.Open
With CreateObject("ADODB.Stream")
.Type = 2: .Open: .WriteText aBuf
.Position = 2: .CopyTo bStream: .Close
End With
bStream.SaveToFile FileName, 2: bStream.Close
Set bStream = Nothing
End Sub
Sub PrintToImmediateWindow(coll As Object)
Dim I As Long
For I = 0 To coll.Count - 1
Debug.Print coll(I)
Next I
End Sub
Sub PrintArrayToImmediate(buf As Variant)
Dim I As Long
For I = LBound(buf) To UBound(arr)
Debug.Print buf(I)
Next I
End Sub
Sub Main()
Dim coll As Object
Set coll = CreateObject("System.Collections.ArrayList")
coll.Add "14, 99, 104, 0, 0, 0, 65, 112, 114, 105,"
coll.Add "255, 255, 104, 76, 43, 32, 23, 56, 77,"
Dim arr As Variant
buf = coll.ToArray
WriteBinary "C:\file.txt", buf
End Sub
What I have tried:
So far I am able to get it working with a smaller buffer, but if I try to use a really large buffer, I get too many line continuation errors, which means I need to break it up