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

Построчное чтение (FSO)

Private Sub ReadTextFileLL()
' Построчное чтение файла (FSO.OpenTextFile)
' Справка: https://analystcave.com/vba-filesystemobject-fso-in-excel/vba-opentextfile/
' -------------------------------------------------------------------------------------------------/
Dim objFSO As Object, objSRCFile As Object
Dim lLine&, sSRCFilePath$
Const csNewFileExt$ = "csv"
' -------------------------------------------------------------------------------------------------/
On Error GoTo ReadTextFileLineByLine_Err

    sSRCFilePath = "d:\Temp\Excel - Импорт текста\я240417.vbs"

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objSRCFile = objFSO.OpenTextFile(sSRCFilePath, 1, False)
    With objSRCFile
        Do While Not .AtEndOfStream
            lLine = lLine + 1
            Debug.Print .ReadLine
        Loop
    End With
    
ReadTextFileLineByLine_End:
    On Error Resume Next
    objSRCFile.Close: Set objSRCFile = Nothing
    Set objFSO = Nothing
    Err.Clear
    Exit Sub

ReadTextFileLineByLine_Err:
    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in Sub :" & _
        "ReadTextFileLineByLine - Module1.", vbCritical, "Error!"
    Err.Clear
    Resume ReadTextFileLineByLine_End
End Sub
Назад ToTop
L.E. 19.04.2024
Рейтинг@Mail.ru