8

I have a binary file that resulted from a program written in Compaq Visual Fortran. How can I read specific lines and save them in an Excel sheet?

1
  • 4
    How does a binary file have "lines"? Commented Mar 27, 2009 at 20:11

3 Answers 3

14

If you want to read the entire file into one big array, you can use the following code:

Dim byteArr() As Byte
Dim fileInt As Integer: fileInt = FreeFile
Open "C:\path\to\my\file.ext" For Binary Access Read As #fileInt
ReDim byteArr(0 To LOF(fileInt) - 1)
Get #fileInt, , byteArr
Close #fileInt

The result is identical to the answer by Todd Owen, but achieved without the use of external libraries.

Sign up to request clarification or add additional context in comments.

Comments

8

Another way is using ADODB.Stream:

With CreateObject("ADODB.Stream")
    .Open
    .Type = 1  ' adTypeBinary
    .LoadFromFile file.Path
    bytes = .Read
    .Close
End With

(Sorry, I'm not actually sure what library it's in, which is why this sample code uses CreateObject and the literal value 1 instead of the named constant adTypeBinary!)

4 Comments

The library is Microsoft Active Data Object.
why using an external library for such a trivial and built-in task?
Refrence Microsoft ActiveX Data Objects x.x Library
Dim bytes As Variant
7

You have to open it using "Binary Access".

See http://www.vbforums.com/showthread.php?t=430424

Sub Temp()
    Dim intFileNum%, bytTemp As Byte, intCellRow%
    intFileNum = FreeFile
    intCellRow = 0
    Open "C:\temp.bin" For Binary Access Read As intFileNum
    Do While Not EOF(intFileNum)
        intCellRow = intCellRow + 1
        Get intFileNum, , bytTemp
        Cells(intCellRow, 1) = bytTemp
    Loop
    Close intFileNum
End Sub

Comments

Your Answer

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