É possível usar o Scripting Runtime object library para manipular arquivos texto. O exemplo abaixo assume que seu projeto VBA acrescentou uma referência à biblioteca Microsoft Scripting Runtime.
Sub WriteToTextFile()Dim fs As Scripting.FileSystemObject, f As Scripting.TextStreamDim l As LongSet fs = New FileSystemObjectSet f = fs.OpenTextFile("C:\FolderName\TextFileName.txt", _ForWriting, True)With fFor l = 1 To 100.WriteLine "This is line number " & lNext l.CloseEnd WithSet f = NothingSet fs = NothingEnd SubSub AppendToTextFile()Dim fs As Scripting.FileSystemObject, f As Scripting.TextStreamDim l As LongSet fs = New FileSystemObjectSet f = fs.OpenTextFile("C:\FolderName\TextFileName.txt", _ForAppending, True)With fFor l = 1 To 100.WriteLine "Added line number " & lNext l.CloseEnd WithSet f = NothingSet fs = NothingEnd SubSub ReadFromTextFile()Dim fs As Scripting.FileSystemObject, f As Scripting.TextStreamDim l As LongSet fs = New FileSystemObjectSet f = fs.OpenTextFile("C:\FolderName\TextFileName.txt", _ForReading, False)With fl = 0While Not .AtEndOfStreaml = l + 1Cells(l, 5).Formula = .ReadLineWend.CloseEnd WithSet f = NothingSet fs = NothingEnd Sub
Nenhum comentário:
Postar um comentário