Option ExplicitPrivate Sub Command1_Click() Dim sFile As String Open "d:\1.txt" For Input As #1 '打开你的文件 Do Line Input #1, sFile '在这里添加你的处理代码 MsgBox sFile '处理后一行一行读出 Loop While Not (EOF(1)) Close #1End Sub
使用FileSystemObject,TextStream 需要添加引用:Microsoft Scripting Runtime 代码如下:Dim fso As New FileSystemObject Dim fileName As String Dim ts As TextStream Dim strReadLine As StringfileName = "FilePath" ' 输入你的 txt文件路径 If fso.FileExists(fileName) Then Set ts = fso.OpenTextFile(fileName, ForReading, False) While Not ts.AtEndOfStream strReadLine = ts.ReadLine Debug.Print strReadLine If Trim(strReadLine) <> "" Then '这里可以添加你的处理代码 else '可以使用下面语句跳过一行 ts.SkipLine End If Wend set ts=nothing End if setfso=nothing
Option ExplicitPrivate Sub Command1_Click()
Dim sFile As String
Open "d:\1.txt" For Input As #1 '打开你的文件
Do
Line Input #1, sFile
'在这里添加你的处理代码
MsgBox sFile '处理后一行一行读出
Loop While Not (EOF(1))
Close #1End Sub
需要添加引用:Microsoft Scripting Runtime
代码如下:Dim fso As New FileSystemObject
Dim fileName As String
Dim ts As TextStream
Dim strReadLine As StringfileName = "FilePath" ' 输入你的 txt文件路径
If fso.FileExists(fileName) Then
Set ts = fso.OpenTextFile(fileName, ForReading, False)
While Not ts.AtEndOfStream
strReadLine = ts.ReadLine
Debug.Print strReadLine
If Trim(strReadLine) <> "" Then
'这里可以添加你的处理代码
else
'可以使用下面语句跳过一行
ts.SkipLine
End If
Wend
set ts=nothing
End if
setfso=nothing