Reading the content of binary file using Visual Basic (VBA)

Edit ArticleEdit Article

The below code snippet demonstrates how to read the binary content into the variable of type Byte() from the specified file in Visual Basic 6 (VBA).

Dim content As Byte()
content = ReadByteArrFromFile("C:\MyFolder\MyFile.dat")

Code will generate an exception if file doesn't exist or cannot be read.

Function ReadByteArrFromFile(filePath) As Byte()

    Dim buff() As Byte
    
    Dim fileNumb As Integer
    fileNumb = FreeFile
    
    Open filePath For Binary Access Read As fileNumb
    
    ReDim buff(0 To LOF(fileNumb) - 1)
    
    Get fileNumb, , buff
    
    Close fileNumb
    
    ReadByteArrFromFile = buff
    
End Function

Notifications

Join session by SOLIDWORKS and PDM API expret Artem Taturevych at 3DEXPERIENCE World 2025 on Feb 26 at 08:30 AM CST to explore 10 essential macros for automating drawings, assemblies, custom properties, and more


Product of Xarial Product of Xarial