Click here to Skip to main content
15,881,413 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

VB
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.


VB
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,"
    'so on and so forth

    Dim arr As Variant
    buf = coll.ToArray
    
    'Print the array
    'Debug.Print vbCrLf & "Printing the array contents:"
    
    'For printing arrays as Variants
    'PrintArrayToImmediate buf
    
    'For printing arrays as coll objects
    'PrintToImmediateWindow coll
    
    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
Posted
Updated 2-Jun-22 15:46pm
v3
Comments
Dave Kreskowiak 2-Jun-22 0:37am    
Are you saying that you want to make this:
buf = Array(184, 97, 61, 32, 0, 4, ....)

with as many values as you want to type into the array?
buf = Array(184, 97, 61, 32, 0, 4, 6, 54, 23, 34 _
        45, 34, 24, 65, 87, 45, 76, ..., _
        56, 26, 86, 65, 87, 9, 84, ..., _
        ... more values ad nauseum )
Franz Schweizer 2-Jun-22 0:47am    
Exactly, Dave. Yes. I am not able to use more than 24 of those line separators and I have an enormous amount of values to enter and I'm just not sure how to go about doing this. If I could somehow append to the file, then I could call my function about a thousand times but if not, I'm not sure what else I could do. This VBA language is very limiting and makes no sense that I can't simply use a long line..
Dave Kreskowiak 3-Jun-22 0:08am    
Yeah, you cannot expect the interpreter to allow unlimited statement length. That's what the line continuation character is doing. Actually, no language that I'm aware of will allow you to use an unlimited number of continuations.

I can't tell what you're really tying to do. In one example, you're writing a bunch of byte values to a file (and calling it a .txt file) and in your other example, you're writing a bunch of text that is NOT equivalent to your first example.

So what's the actual goal? What needs to be in the file? Is it bytes or text?
Franz Schweizer 4-Jun-22 21:32pm    
any ideas?
Dave Kreskowiak 4-Jun-22 21:41pm    
Since you didn't reply to one of my posts, I had no idea you posted anything.

Dim buf As String()
buf(0) = "This is a string"
buf(1) = "This is another string"

As for byte data, I have no idea how you're representing your data so it's difficult to say.

If the data is the same between all copies of the file, you can just create your data in an actual file and copy it to a new file when needed.

1 solution

Quote:
Join arrays in VBA script to write to file

The solution: don't join.
your code is OK with unlimited memory, which is not the case with your computer.
Look at your code, you join elements of an array only to write it is a single go in a file, and then discard the joined variable.

This way of coding will work with any size of buf and is much faster
VB
Sub WriteBinary(FileName, buf)
   Dim I, aBuf, Size, bStream
    Size = UBound(buf)
    ' Open the file
    For I = 0 To Size - 1 Step 2

         Tmp = ChrW(buf(I + 1) * 256 + buf(I))
         ' Write Tmp to file
    Next
    If I = Size Then
        Tmp = ChrW(buf(I))
        ' Write Tmp to file
    End If
    ' Close file
End Sub

[UpDate]
Quote:
Join arrays in VBA script to write to file

Your problem is not about joining array.
Your problem is about source not compiling and you have been carefull to remove offending code.
Do not wonder why you get answer on wrong topic.
 
Share this answer
 
v2
Comments
Franz Schweizer 1-Jun-22 22:36pm    
not able to use this function at all. missing half of it to even write to a file. also, even if my function is able to use any amount of buf, in vba, i physically cannot have a huge array. it just doesn't work, throws a compile error. whoever programmed it does not let you use an enormous array or string and i can only use 24 of those line separators which would not be enough. i need either a way to concatenate strings such as

buf = "97,97,97,97"
buf = buf & "77,241,45,54"
buf = buf & ...

and then turn this concatenation into a true Variant array that I can then pass off to my function or some way for me to keep calling the function and it append to the file. My functions works perfectly fine. I just have to bypass these vba limitations with the length of an array. Any ideas?
Patrice T 1-Jun-22 22:44pm    
"missing half of it to even write to a file."
Comments should give you enough hints to write the missing code by yourself.
Patrice T 1-Jun-22 22:47pm    
Use Improve question to update your question.
So that everyone can pay attention to this information.
Franz Schweizer 1-Jun-22 22:56pm    
I added the bottom half, writing Tmp instead of aBuf but it only wrote 2 bytes of 0xff to the file.
Patrice T 1-Jun-22 23:07pm    
Use Improve question to update your question.
Show your new code too.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900