Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ' ---
- ' Write byte array as file
- ' ---
- ' http://www.visualbasic.happycodings.com/Files_Directories_Drives/code52.html
- 'Purpose : Saves/writes a block of data to a file
- 'Inputs : vData The data to store in the file. Can be an
- ' array or any simple data type.
- ' sFileName The path and file name where the data is to be stored
- ' [bAppendToFile] If True will append the data to the existing file
- 'Outputs : Returns True if succeeded in saving data
- 'Notes : Saves data type (text and binary).
- Function WriteByteArray(vData As Variant, sFileName As String, Optional bAppendToFile As Boolean = False) As Boolean
- Dim iFileNum As Integer, lWritePos As Long
- Debug.Print " --> Entering WriteByteArray function with " & sFileName & " file to write."
- On Error GoTo ErrFailed
- If bAppendToFile = False Then
- If Len(Dir$(sFileName)) > 0 And Len(sFileName) > 0 Then
- 'Delete the existing file
- VBA.Kill sFileName
- End If
- End If
- iFileNum = FreeFile
- Open sFileName For Binary Access Write As #iFileNum
- If bAppendToFile = False Then
- 'Write to first byte
- lWritePos = 1
- Else
- 'Write to last byte + 1
- lWritePos = LOF(iFileNum) + 1
- End If
- Put #iFileNum, lWritePos, vData
- Close iFileNum
- WriteByteArray = True
- Exit Function
- ErrFailed:
- Debug.Print "################################"
- Debug.Print "Error handling of WriteByteArray"
- Debug.Print "################################"
- FileWriteBinary = False
- Close iFileNum
- Debug.Print Err.Description & "(" & Err.Number & ")"
- End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement