VBA, MS Access MS Access в примерах

Построчное чтение (FSO) - C записью строк в массив

Private Sub Test001()
'собственно парсинг одного файла
Dim sVal$
    sVal = CurrentProject.Path & "\Sample.txt"
    ParseOneFile sVal

End Sub

Private Sub ParseOneFile(sFilePath)
'es - 16.10.2022 - Построчное чтение текстового файла с записью строк в массив FileLines()
'---------------------------------------------------------------------------------------------------
Dim FSO As Object      'FileSystemObject
Dim FSOFile As Object  'File
Dim TextStream As Object
Dim FileLines() As String
Dim LineValues() As String

Dim lVal As Long, sVal As String
Dim iVal%, lPos As Long
'---------------------------------------------------------------------------------------------------
On Error GoTo ParseOneFile_Err
   
   Set FSO = CreateObject("Scripting.FileSystemObject")
   Set FSOFile = FSO.GetFile(sFilePath)
   Set TextStream = FSOFile.OpenAsTextStream(1) 'OpenFileForReading = 1
   
    Do While Not TextStream.AtEndOfStream
        sVal = sVal & TextStream.ReadLine & vbNewLine
    Loop
    
    TextStream.Close
    FileLines = Split(sVal, vbNewLine, -1, vbTextCompare)

    For lVal = LBound(FileLines) To UBound(FileLines)
        Debug.Print Format(lVal, "000000") & " =  " & FileLines(lVal)
        
        LineValues() = Split(FileLines(5), "|")
        For iVal = LBound(LineValues) To UBound(LineValues)
            Debug.Print iVal & " = " & LineValues(iVal)
        Next iVal
    Next lVal
    

'---------------------------------------------------------------------------------------------------
ParseOneFile_End:
    On Error Resume Next
    Set FSO = Nothing
    Set FSOFile = Nothing
    Set TextStream = Nothing
    Err.Clear
    Exit Sub

ParseOneFile_Err:
    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in Sub : " & _
           "ParseOneFile - modParseOrders.", vbCritical, "Error!"
    'Debug.Print "ParseOneFile_Line: " & Erl & "."
    Err.Clear
    Resume ParseOneFile_End
End Sub
Назад ToTop
L.E. 19.04.2024
Рейтинг@Mail.ru