Advertisement
Guest User

Untitled

a guest
Sep 17th, 2012
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. ' ---
  2. ' Write byte array as file
  3. ' ---
  4. ' http://www.visualbasic.happycodings.com/Files_Directories_Drives/code52.html
  5.  
  6. 'Purpose : Saves/writes a block of data to a file
  7. 'Inputs : vData The data to store in the file. Can be an
  8. ' array or any simple data type.
  9. ' sFileName The path and file name where the data is to be stored
  10. ' [bAppendToFile] If True will append the data to the existing file
  11. 'Outputs : Returns True if succeeded in saving data
  12. 'Notes : Saves data type (text and binary).
  13.  
  14.  
  15. Function WriteByteArray(vData As Variant, sFileName As String, Optional bAppendToFile As Boolean = False) As Boolean
  16. Dim iFileNum As Integer, lWritePos As Long
  17.  
  18. Debug.Print " --> Entering WriteByteArray function with " & sFileName & " file to write."
  19. On Error GoTo ErrFailed
  20. If bAppendToFile = False Then
  21. If Len(Dir$(sFileName)) > 0 And Len(sFileName) > 0 Then
  22. 'Delete the existing file
  23. VBA.Kill sFileName
  24. End If
  25. End If
  26.  
  27. iFileNum = FreeFile
  28. Open sFileName For Binary Access Write As #iFileNum
  29.  
  30. If bAppendToFile = False Then
  31. 'Write to first byte
  32. lWritePos = 1
  33. Else
  34. 'Write to last byte + 1
  35. lWritePos = LOF(iFileNum) + 1
  36. End If
  37.  
  38. Put #iFileNum, lWritePos, vData
  39. Close iFileNum
  40.  
  41. WriteByteArray = True
  42. Exit Function
  43.  
  44. ErrFailed:
  45. Debug.Print "################################"
  46. Debug.Print "Error handling of WriteByteArray"
  47. Debug.Print "################################"
  48. FileWriteBinary = False
  49. Close iFileNum
  50. Debug.Print Err.Description & "(" & Err.Number & ")"
  51. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement