Find and Highlight Substring

By | 2002-06-01

‘ Given an editable textbox named Text1, this code prompts to find a word and
‘ searches throught the textbox and highlights the first occurance of the
‘ found word (if exists).

Original Author: Bill Chelonis

Code

Private Sub FindFunction_Click()
Rem Find/highlight first occurance of a word in a textbox named Text1
Dim a As String
Dim y As Integer
a = InputBox("Find text: ", "Find", "")
Call Text1.SetFocus
SendKeys ("^{HOME}")
y = 1
Do Until y = Len(Text1.text)
Rem check if word was located
If Mid(UCase$(Text1.text), y, Len(a)) = UCase$(a) Then
   Rem highlight the found word and exit sub
   For x = 1 To Len(a)
    SendKeys ("+{RIGHT}")
   Next x
   Exit Do
End If
Rem do nothing if carriage return encountered else highlight found word
If Mid(Text1.text, y, 1) = Chr$(13) Then
Else
Rem move the cursor to the next element of text
SendKeys ("{RIGHT}")
End If
y = y + 1
If y > Len(Text1.text) Then Exit Do
Loop
End Sub

Author: dwirch

Derek Wirch is a seasoned IT professional with an impressive career dating back to 1986. He brings a wealth of knowledge and hands-on experience that is invaluable to those embarking on their journey in the tech industry.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.