Richtextbox search

Posted on 2000-04-03
Last Modified: 2012-08-14
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
Question by:Geo24
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +3
LVL 12

Accepted Solution

mark2150 earned 100 total points
ID: 2680189
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.



Expert Comment

ID: 2680217
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.
LVL 10

Expert Comment

ID: 2680232
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now


Expert Comment

ID: 2680331
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
LVL 12

Expert Comment

ID: 2680387
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!

LVL 10

Expert Comment

ID: 2680399

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?

Expert Comment

ID: 2680455
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

Expert Comment

ID: 2681129
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


LVL 10

Expert Comment

ID: 2681799
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!

Expert Comment

ID: 2681970
TheMask changed the proposed answer to a comment
LVL 12

Expert Comment

ID: 2682327

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!


Author Comment

ID: 2683056
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.
LVL 12

Expert Comment

ID: 2683426
Simple enough.

const quote as string * 1 = """"

str1 = "color=" & quote
str2 = quote

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


Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month7 days, 2 hours left to enroll

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question