您好我正在尝试在文本框中搜索单词。一旦找到该单词,我希望系统在找到的单词后面找到接下来的7个单词。基本上,每次在这个文本框中复制paragarph并且有一个单词(User ID :)将始终存在。但每次都会在“用户ID:”后面输入不同的用户ID。我想将这个自动化系统找出来。我可以使用下面的代码找到“用户ID”一词。但我不确定如何得到它后面的实际用户ID。
Dim Search, Where
' Get search string from user.
Search = "User ID:"
' Find string in text.
Where = InStr(TxtWebForm.Text, Search)
If Where Then
TxtWebForm.SetFocus
TxtWebForm.SelStart = Where - 1
TxtWebForm.SelLength = Len(Search)
Else
MsgBox "String not found."
End If
答案 0 :(得分:0)
您可以查找接下来的9个空格(7个字加上#34中的一个空格;用户ID"加上&#34之后的一个空格;用户ID")。
Private Sub txtWebForm_Change()
Dim Search As String
Dim Where As Long
Dim LastSpace As Long
Dim i As Long
Search = "User ID:"
Where = InStr(1, Me.txtWebForm.Text, Search)
LastSpace = Where
For i = 1 To (7 + 2)
LastSpace = InStr(LastSpace + 1, Me.txtWebForm.Text, Space(1))
Next i
With Me.txtWebForm
.SetFocus
.SelStart = Where - 1
.SelLength = LastSpace - .SelStart
End With
End Sub