Here is a simple way of reading a text file character by character. This code relies on the fact that the file is a Windows (CRLF) coded file. This means that each line is terminated by carriage return (CR) - CHR(13) - and line feed - CHR(10) - (LF). As the routine is implemented as a Public function it can be called from an Excel sheet eg =fnGetLine(2)
Option Explicit
Sub sbTest()
MsgBox fnGetLine(2)
End Sub
Public Function fnGetLine(iLineNum As Integer) As String
Dim sLines(99) As String
If iLineNum > 99 Then Exit Function
Dim iLines As Integer
Dim sChar As String
Dim ii As Long
Dim iTxtFile As Integer
Dim strFileName As String
Dim strFileText As String
strFileName = ActiveWorkbook.Path & "\username.txt"
iTxtFile = FreeFile
Open strFileName For Input As FreeFile
strFileText = Input(LOF(iTxtFile), iTxtFile)
Close iTxtFile
iLines = 1
sLines(1) = ""
For ii = 1 To Len(strFileText) Step 1
sChar = Mid(strFileText, ii, 1)
If sChar = Chr(13) Then
If Mid(strFileText, ii + 1, 1) = Chr(10) Then
iLines = iLines + 1
sLines(iLines) = ""
ii = ii + 1
Else
Exit Function ' error
End If
Else
sLines(iLines) = sLines(iLines) & sChar
End If
Next ii
fnGetLine = sLines(iLineNum)
End Function
The encodings supported by Notepad are -
ANSI - American National Standards Institute,
UTF-16 LE - Little endian,
UTF-16 BE - Big endian,
UTF-8 - the default on Windows 11,
UTF-8 with BOM
BOM = byte order mark - https://learn.microsoft.com/en-us/globalization/encoding/byte-order-mark
https://en.wikipedia.org/wiki/Windows_code_page
Microsoft adopted a Unicode encoding (first the now-obsolete UCS-2, which was then Unicode's only encoding), i.e. UTF-16 for all its operating systems from Windows NT onwards, but additionally supports UTF-8 (aka CP_UTF8) since Windows 10 version 1803.