I'm quite frustrated trying to write to a binary file in Excel VBA. The issue is with fixed-length string data in a Type structure. (Note that my binary file requirements are that fixed-length string data, if shorter than the actual field length, must be padded with nulls rather than with spaces.)

Sometimes I can assign the fixed-length string directly and it works just fine (when my source string is shorter and I've padded it with null characters for length). Other times (when the source string is an exact match for the length of the target without null padding), if I try to assign the fixed-length string directly, it fails by assigning half question marks and half spaces to the fixed-length string, instead, and I must use byte arrays as the source to work around the problem.

Note that I'm aware of the default encoding difference between VBA standard variable-length strings (UTF-16) and fixed-length strings in a Type structure for binary files (ANSI).

So... Are there rules somewhere for the best practice in assigning VBA string data to the fixed-length string elements of a Type structure that is used to read/write a binary file?

I presented this problem to an AI and received the advice that a Type structure should always use byte arrays, internally, instead of fixed-length strings. Is that good advice? Or, is there another way?

For something to look at, the following are my current Type structures and I have everything working--just looking for some good advice.

Private Type schedulerHeader    ' reclen = 100
    Version     As String * 100
End Type
Private Type schedulerEntry     ' reclen = 329
    Name        As String * 100
    Event       As String * 10
    BegDate     As LongLong '8
    Frequency   As String * 1
    Occurs      As Long     '4
    Repetitions As String * 24
    RunDate     As LongLong '8
    EndDate     As LongLong '8
    RetCode     As Long     '4
    NextDate    As LongLong '8
    EntryPid    As String * 10
    StartSys    As String * 30
    StartName   As String * 100
    StartPid    As String * 10
    ParmLength  As Long     '4
End Type

2 Replies 2

Did you try to use Microsoft ActiveX Data Object Library in your VBA code? MS ADO has Stream class which has a property Type which can be set to adTypeBinary and Charset. It is e.g. here "C:\Program Files (x86)\Common Files\System\ado\msado15.dll".

I did not. I used the built-in OPEN, GET, PUT, and CLOSE statements; because, I was told they were faster. However, I just asked the AI specifically about binary stream I/O (answer follows). So, as best practice, I guess it is better to use ADO for binary output. Thanks.

For binary stream I/O in Excel VBA, the MS ADO (ActiveX Data Objects) Stream object is generally the superior choice over the built-in OPEN, GET, PUT, and CLOSE statements. ADO Stream is faster, more reliable, and offers greater flexibility, particularly with file handling and character encoding.

Your Reply

By clicking “Post Your Reply”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.