How to write a string array to a text file
Posted on January 5, 2009
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | 'File/Folder Handling - How to write a string array to a text file Option Explicit Public Function WriteArrayToFile(ByRef strArray() As String, ByVal FilePath As String) As Boolean Dim fileNum As Long Dim I As Integer On Error GoTo WriteArrayToFile_Error fileNum = CreateFile(FilePath) For I = LBound(strArray) To UBound(strArray) Print #fileNum, strArray(I) Next I Call CloseFile(fileNum) WriteArrayToFile = True ExitHere: On Error GoTo 0 Exit Function WriteArrayToFile_Error: WriteArrayToFile = False MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure WriteArrayToFile of Form Form1" End Function Public Function OpenFile(ByVal FilePath As String) As Long On Error GoTo OpenFile_Error If Len(Dir$(FilePath)) = 0 Then OpenFile = -1 Exit Function End If OpenFile = FreeFile Open FilePath For Input As #OpenFile ExitHere: On Error GoTo 0 Exit Function OpenFile_Error: OpenFile = -2 MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure OpenFile of Form Form1" End Function Public Function CloseFile(ByVal FileNumber As Long) As Boolean On Error GoTo CloseFile_Error Close #FileNumber CloseFile = True ExitHere: On Error GoTo 0 Exit Function CloseFile_Error: CloseFile = False MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure CloseFile of Form Form1" End Function |