Richtextbox search

I want a function that searches the word in between two tags like <font>red</font> the fastest way possible in vb. You should be able to call it like this,,

dim str1,str2 as string
call SearchBetween(RTF1,str1,str2,output)

the string should be able to be anything like.. blablabla and hello

And fast as possible
Who is Participating?
mark2150Connect With a Mentor Commented:
public function isolate(rtb as string, str1 as string, str2 as string) as string
marker1 = instr(1, rtb, str1, vbTextCompare)
if marker1 < 1 then exit function
marker2 = instr(marker1, rtb, str2, vbtextCompare)
if marker2 < 1 then exit function
isolate = mid( rtb, marker1 + len(str1) + 1, str2-marker1 + len(str1) -1)
end function

This should be close for off the top of my head.


If there might be multiple instances of the string you are searching for (and you are usign VB6) then you could use the Filter function.
Otherwise, you could use the InStr function.
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Hi, this code is your straight forward solution,
Note: if you didn't put either the StartTag or EndTag the code returns ""

Private Sub SearchBetween(ByVal strInput As String, ByVal strStartTag As String, ByVal strEndTag As String, strOutput As String)
Dim StartTagPosition As Long, EndTagPosition As Long
StartTagPosition = InStr(1, strInput, strStartTag, vbTextCompare)
StartTagPosition = StartTagPosition + Len(strStartTag) - 1
EndTagPosition = InStr(1, strInput, strEndTag, vbTextCompare)
If (StartTagPosition And EndTagPosition) = 0 Then strOutput = "": Exit Sub
strOutput = Mid(strInput, StartTagPosition + 1, EndTagPosition - StartTagPosition - 1)
End Sub
Hey TheMask!

Your answer is almost identical to my comment!

Tsk, tsk, tsk! It's considered "poor form" to post an ANSWER that is the same or nearly so to a previously posted COMMENT!


Yes, I agree. But TheMask is quite new (first login 25 March 2000) so give TheMask a chance...

TheMask, you should really check previously posted comments and answers before you post your own suggested answer. It is commonly accepted practice here, out of politeness, not to lock the question with an answer if it is very similar to something posted earlier.

You can however withdraw your answer. Would you kindly do so?
Private Function SearchBetween(rtb As String, tagStart As String, tagEnd As String, schval As String)
On Error Resume Next
Dim tmpVal As String

startVal = 1
tmpVal = Mid(rtb, InStr(1, rtb, tagStart) + Len(tagStart), InStr(1, rtb, tagEnd) - InStr(1, rtb, tagStart) - Len(tagStart))
If InStr(tmpVal, schval) Then
    MsgBox tmpVal
End If
End Function
To mark2150, caraf_g and all:
Sorry guys for the unpolite behaviour,i didn't think of that point
but i thought that as long as i am sure of my answer i have to make it an answer.

To Geo24:  don't give me the points if you i did what you want cuz mark2150 did the same and he was quicker too


Hi TheMask, thanks for coming back on this. You don't have to ask Geo24 to reject the answer. If you look at the thread, you'll see you have the option to withdraw your answer. Anyway - again, welcome to EE. Hope you'll enjoy it as much as we do!
TheMask changed the proposed answer to a comment

Welcome to E-E. It's not really about the points - it's about helping. But we are a prickley bunch and we've developed some ground rules.

Myself, I essentially NEVER post "answers", it shuts down the dialog on the Q. If the poster likes my response they use the "Accept Comment as Answer" feature and award the points that way.

Always read the threads. Not only do you see what is and what isn't already in there, but you can pick up some good tips as well.

In fact even tho I'm a top expert, hardly a day goes by that I don't learn a new trick. Just today I found out something about the DIR command that I didn't know! I was in the process of telling someone how their post was wrong when I read it closely, saw something I'd not noticed, checked with the help and discovered that the "bad" code I was about to pan was faster and more efficient than my "good" code! (Erick37!)

Another tip - don't answer Q's that are obvously "homework". Q's that start out "Describe in detail..." or "Contrast and compare..." or "Create a program that..." We are not a homework service and people that post that kind of Q usually get howled down.

E-E is a strong supporter of copyright laws and feel free to report anyone asking how to beat a copy protection or licensing scheme.

Anyway, E-E is a fun place to hang out. We have a lot of good technical stuff and some great people. Be sure to check out the Lounge if you need a break from "geek speak".

Again, welcome!

Geo24Author Commented:
But what if there is for example a " and " on each side, then it dont return nothing. Like, color="red" and i want red. Otherwise its great, thanx! And thanx to all others to.
Simple enough.

const quote as string * 1 = """"

str1 = "color=" & quote
str2 = quote

This will find the word *red* in the middle of color="red"

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.