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

Product of Xarial Product of Xarial