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

Edit ArticleEdit Article
Comments

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

All articles and code at CodeStack are now open-source and hosted on GitHub. If you want to contribute by modifying existing articles and code snippets, submitting new ones, reporting errors and bugs etc. please follow this blog post for more information. We appreciate any contribution.


Product of Xarial Product of Xarial