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
  • 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

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?
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!


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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

746 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now