vba read text file code example
Example 1: excel vba open text file
Function OpenTextFile$(f)
With CreateObject("ADODB.Stream")
.Charset = "utf-8"
.Open
.LoadFromFile f
OpenTextFile = .ReadText
End With
End Function
s = OpenTextFile("C:\123.json")
MsgBox Len(s)
Example 2: vba get text file content
Function FileInArray() As Variant
Dim fileSO As Object, textFile As Object, content
On Error GoTo CExit
Set fileSO = CreateObject("Scripting.FileSystemObject")
Set textFile = fileSO.OpenTextFile("C:\temp\file.txt", 1)
content = textFile.ReadAll
FileInArray = Split(content, vbNewLine)
textFile.Close
CExit:
End Function