本人诚心问一问题,希望大家看到后赐教,万分感谢。
说下我的内容
有一个txt
Open "c:\1.txt" For Input As #1
这个TXT是多行内容。
我这样把它读出来,赋予变量
然后查找这个变量里包括一个字符串的内容,比如这个字符串是123,查到后,把包括这个123字符串的当前行全部输出出来给某变量(然后我自己再把变量让text1.text=变量显示出来或者其他动作)。
请问如何实现。。。
Sub Test()
Dim w1 As String, i As Long, ww
w1 = ReadText("d:\ttt.txt")
If w1 <> "" Then
ww = Split(w1, vbCrLf)
For i = 0 To UBound(ww)
If InStr(1, ww(i), "123", vbTextCompare) > 0 Then
MsgBox ww(i) & " 包含123!"
Exit For ''找到一个后立即退出循环
End If
Next
End If
End Sub
Function ReadText(ByVal FullNames As String) As String
On Error GoTo errs
Dim fso As New FileSystemObject, bFile As File, w1 As String
ReadText = fso.GetFile(FullNames).OpenAsTextStream.ReadAll
errs:
Set fso = Nothing
End Function
Private Sub test()
Dim s$
Open "c:\1.txt" For Input As #1
s = StrConv(InputB$(LOF(1), #1), vbUnicode)
Close #1
With CreateObject("vbscript.regExp")
.Pattern = "^.*?123.*?$" '表示包含123的行
.MultiLine = True
If .Execute(s).Count > 0 Then MsgBox .Execute(s)(0)
End With
End Sub
Dim strTmp As String, strLine As String
Open "c:\1.txt" For Input As #1
Do Until EOF(1)
Line Input #1, strLine
If InStr(strLine, "123") Then StrTpm = strTmp & IIf(strTmp > "", vbCrLf, "") & strLine
Loop
Close #1
Text1 = strTmp