vb 读取txt文件内容
发布网友
发布时间:2023-06-30 16:09
我来回答
共2个回答
热心网友
时间:2023-10-08 22:25
函数:
Function FileRead(PathFile As String, Optional Fore As String, Optional Back As String) As String
'════════取得文件中的部分或全部内容════════
''该函数传回原文件中指定区域的字符串。
''PathFile 字符串,文件的实际路径全名。
''Fore、Back 字符串,可选。开始位置后;结束位置前。
''如果省略 Fore 和 Back 则返回所有原始字符串;
''如果 Fore 不空,Back 空,返回开始之后的内容;
''如果 Back 不空,Fore 空,返回结束之前的内容;
''如果 Fore 和 Back 都不空,返回指定的中间部分;
''如果 Fore 和 Back 任意一个不空并找不到,返回空值。
'═════════════════════════════
If PathFile = "" Or IsNull(PathFile) Then Exit Function
Fore = CStr(Fore): Back = CStr(Back)
Dim Fso, Ts, TStr As String, Tmp1, Tmp2
On Error Resume Next
Set Fso = CreateObject("Scripting.FileSystemObject")
If Err <> 0 Then
Exit Function
End If
If Fso.FileExists(PathFile) = False Then Exit Function
Dim TI1 As Long, TI2 As Long
Set Ts = Fso.OpenTextFile(PathFile, 1, False)
If Not Ts.AtEndOfStream Then
TStr = Ts.ReadAll
End If
Set Ts = Nothing
If TStr = "" Or (Fore = "" And Back = "") Then
FileRead = TStr
Exit Function
End If
If Fore <> "" Then TI1 = InStr(1, TStr, Fore, 1)
If Back <> "" Then TI2 = InStr(TI1 + Len(Fore) + 1, TStr, Back, 1)
If (TI1 > 0 And TI2 > TI1) Or _
(Fore <> "" And Back <> "") Then
If TI1 = 0 Or TI2 = 0 Then Exit Function
TI1 = TI1 + Len(Fore)
TI2 = TI2 - TI1
FileRead = Mid(TStr, TI1, TI2)
ElseIf TI1 > 0 And TI2 = 0 Then
TI1 = TI1 + Len(Fore)
FileRead = Mid(TStr, TI1, Len(TStr) - TI1)
ElseIf TI1 = 0 And TI2 > 0 Then
FileRead = Left(TStr, TI2 - 1)
Else
FileRead = TStr
End If
End Function
热心网友
时间:2023-10-08 22:26
private sub command1_click()
open "1.txt" for input as #1
line input #1,aspect
text1.text=aspect
nl=chr(13)&chr(10)
do while not eof(1)
line input #1,aspect
whole=whole & nl & aspect
loop
text2.text=whole
close #1
end sub
必要前提:第一行必须为标题。
text1为标题text2为内容,最好不同的内容存放在不同的文本文件中
要达到你这样的要求比较麻烦,还是多几个按钮比较好