本代码简单,读取出TXT文本文件中的内容,通常叫做记事本,这里使用到了vba中的open对象,并使用Do While Not EOF遍历文本内容,实现完整读取TXT的功能。
代 码:
Dim MyString As String
Dim strFile
Me.txt路径 = Null
With FileDialog(msoFileDialogFilePicker)
.Title = "选择文件"
.InitialFileName = ""
.Filters.Clear
.Filters.Add "TXT", "*.txt"
If .Show Then
strFile = .SelectedItems(1)
End If
End With
Me.txt路径 = strFile
Me.txt内容 = Null
If Not IsNull(Me.txt路径) Then
Open Me.txt路径 For Input As #1
Do While Not EOF(1)
Input #1, MyString
Me.txt内容 = Me.txt内容 & MyString & Chr(13) & Chr(10)
Loop
Close #1
End If
示 例:
点击下载此附件
演 示: