IO

The VBA code fragments below are included in the Code-VB fragments library. Fragments are snippets of code you can insert in your procedures. Each item between braces correspond to a variable of the given type. They are replaced with the new or existing variables by Fragment Builder in Code-VB tools.

Batch Process Multiple Files

'   Create a FileSearch object
Set {Application.FileSearch:FS} = Application.FileSearch
With {Application.FileSearch:FS}
    .LookIn = {STRING:FilePath}  'e.g. "c:\myfolder\"
    .FileName = {STRING:FileSpec}  'e.g. "text??.txt"
    .Execute
End With

'   Loop through the files and process them
For i = 1 To {Application.FileSearch:FS}.FoundFiles.Count
    
Next i

Text File Append

{INTEGER:Fn}  = FreeFile
Open {STRING:Filename} For Append Access Write As #{INTEGER:Fn}
        
Print #{INTEGER:Fn}, {STRING:Output}

Close #{INTEGER:Fn}

Text File Output

{INTEGER:Fn}  = FreeFile
Open {STRING:Filename} For Output Access Write As #{INTEGER:Fn}
        
Print #{INTEGER:Fn}, {STRING:Output}

Close #{INTEGER:Fn}

Text File Read In One Go

{INTEGER:Fn}  = FreeFile
Open {STRING:Filename} For Input As #{INTEGER:Fn}

{STRING:FileContent} = Input(LOF({INTEGER:Fn}), {INTEGER:Fn})
        
Close #{INTEGER:Fn}

Text File Read Multiple Lines

{INTEGER:Fn}  = FreeFile
Open {STRING:Filename} For Input As #{INTEGER:Fn}

Do While Not EOF(Fn)
    Line Input #Fn, Line
    
Loop
        
Close #{INTEGER:Fn}

IO Text File Read Multiple Lines