Link to home
Start Free TrialLog in
Avatar of thomas_mathiesen
thomas_mathiesen

asked on

Find text in Richtextbox and then add it to a string

Dear all,

I am doing the following:
-Find something in a richtextbox control.
   a=richtextbox.find(Searchfor,0)
-Find somethine else in the same richtextbox control.
   b=richtextbox.find(Searchfor2,a)

Then I'll get two numbers.. a=index of the first found and b=index of the second found.
My problem is then to put this into a string.
What I've done is;

String=mid(richtextbox.text,a,b-a)

The textbox control downloads text from webpages, and with some of them it works fine, so I wonder if a richtextbox control may have hidden characters for the mid function?

Please help =)
Avatar of wileecoy
wileecoy
Flag of United States of America image

Actually, the RTB does have hidden chrs, but only if you were using RichTextBox.RTFText.

Since you are using .Text, it will be what is displayed in the RTB.

Since you are doing webpages, might it be the html tags that are in your way?

I have done a lot with this.  I have a whole app just for displaying RTB's and collecting data dynamically from each file.

I use the find function in addition to the InStr and InStrRev functions to get the position of certain text.

If you can post your code and I can test it, I am close to 100% sure that I can figure it out (much sweat getting my app to work).

Let me know if it's the html tags, or post your code and I will test.

Also - let me know if e-mail would be better.

Thanks

Wileecoy.
Avatar of Anthony Perkins
If all you want to do is concatenate to two string than this would be the way to go:

Debug.Print Mid(RichTextBox.Text, a, Len(Searchfor)) & Mid(RichTextBox.Text, b, Len(Searchfor2))

Anthony
Avatar of thomas_mathiesen
thomas_mathiesen

ASKER

Wileecoy,
sounds good, but first let me ask u;
Can I turn off the RTB function in the textbox? I need to use the richtextbox control, because the normal textbox cannot contain the number of characters I need.
And, yes, I am looking for tags, and I need to rip text between two tags. That's why I first find the first and then the second. At the end; grab the string between these two.

I'll send u the code (email).

Acperkins,
What I do:
Text: "blah1 blah2 blah3 blah4"
a=Find"blah1"
b=Find"blah4"
String=mid(textbox,a,b-a)
So that the string will be "blah1 blah2 blah3 balh4"
Wileecoy,
sounds good, but first let me ask u;
Can I turn off the RTB function in the textbox? I need to use the richtextbox control, because the normal textbox cannot contain the number of characters I need.
And, yes, I am looking for tags, and I need to rip text between two tags. That's why I first find the first and then the second. At the end; grab the string between these two.

I'll send u the code (email).

Acperkins,
What I do:
Text: "blah1 blah2 blah3 blah4"
a=Find"blah1"
b=Find"blah4"
String=mid(textbox,a,b-a)
So that the string will be "blah1 blah2 blah3 balh4"
What is ur email Wileecoy?
What is ur email Wileecoy?
What is ur email Wileecoy?
I suspect that Wileecoy is right about the HTML characters, but to answer your question
>>
What I do:
Text: "blah1 blah2 blah3 blah4"
a=Find"blah1"
b=Find"blah4"
String=mid(textbox,a,b-a)
So that the string will be "blah1 blah2 blah3 balh4"
<<

If a < b Then
 String = mid(textbox, a, b + Len("blah4") - 1)
else
 String = mid(textbox, b, a + Len("blah1") - 1)
end if
Please send to JakeAustinLane@aol.com

Thanks.

Wileecoy.

p.s. - I will post any pertinent code so that anyone in the future who 'buys' this question will be able to see the progress and resolution.

Here's the code:
I have a richtextbox control (txttemp).

Public Sub FindText()
On Error GoTo FindTextERR
Dim FoundFile As String
Dim a, b As Long
a = 0
b = 0
Do While a > -1
    a = frmMain.txtTemp.Find("a href", b)
    b = frmMain.txtTemp.Find(">", a)
    FoundFile = Mid(frmMain.txtTemp.Text, a, b - a)
    DoEvents
Loop
Exit Sub
FindTextERR:
End Sub

I am looking for files linked on a webpage, which should be the FoundFile string.
I suspect that a and b get different index'es than u could use in a Mid function.
Any idea?
Do you think has something to do with it?
http://support.microsoft.com/support/kb/articles/Q284/9/48.ASP

/Thomas
This is a sample from this page:
     <a class="topMenuLink" href="http://www.devx.com/free/premierclub/premierclub.asp" target="_top">PremierClub</a>

If all you need is http://www.devx.com/free/premierclub/premierclub.asp

Than
a = 0
b = 0
Do While a > -1
   a = frmMain.txtTemp.Find("href=", b)
   b = frmMain.txtTemp.Find("""", a + Len("href=") + 1)
   FoundFile = Mid(frmMain.txtTemp.Text, a, b - a)
   DoEvents
Loop
thomas,

What the find method does is returns the character position of the beginning of your search text.

So - you will need to include enough padding to only extract the text you want... for example... the following worked for me.

<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:w="urn:schemas-microsoft-com:office:word"
xmlns="http://www.w3.org/TR/REC-html40">

<head>
<meta http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<meta name=ProgId content=Word.Document>
<meta name=Generator content="Microsoft Word 9">
<meta name=Originator content="Microsoft Word 9">
<link rel=File-List href="./computers_files/filelist.xml">
<link rel=Edit-Time-Data href="./computers_files/editdata.mso">
<!--[if !mso]>
<style>
v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style>
<![endif]-->
<title>Computer Features</title>
<!--[if gte mso 9]><xml>


I had to change the code to:

Public Sub FindText()
On Error GoTo FindTextERR
Dim FoundFile As String
Dim a, b As Long
a = 0
b = 0
Do While a > -1
   a = frmMain.txtTemp.Find("a href=", b)
   b = frmMain.txtTemp.Find(">", a)
   FoundFile = Mid(frmMain.txtTemp.Text, a + 8, b - 8 - a)
   DoEvents
Loop
Exit Sub
FindTextERR:
End Sub


Also - I would suggest using a search string that is exclusive but all inclusive to your needs.

By this I mean that you should include 'a href=' as a opposed to 'a href'.  the difference will be your word padding in the Mid function, but also the possibility of having the wrong spacing if there is a new or old html tag that doesn't follow the same format as your search string.

Anyway - hope this helps.

hth.

Wileecoy.
thomas,

forgive me for bad coding - I would never suggest what I posted above:

FoundFile = Mid(frmMain.txtTemp.Text, a + 8, b - 8 - a)


I was simply stepping through the code and changing it as I went.

It is better to incorporate code more conducive to dynamic functioning.

For example...

dim sFindStr as string

sFindStr = "a href="

....etc

FoundFile = Mid(frmMain.txtTemp.Text, a + len(sFindStr), b - len(sFindStr) - a)

....etc

apologies as my tail is clearly between my legs.

Wileecoy.
Guys, I will check this tomorrow.. busy today.
Wileecoy,
You showed me just what I had already done.
The thing is that if u download a textfile that comes from a unix box, (using the inet control), the text in the richtextbox and the character's index is not correct. That means that using the mid(string) will see another string than the textbox sees.
Microsoft has tried to explain the problem for me, so what I need is someone that knows how to convert this string into the same format as the richtextbox is.

A very easy example:

Put a few lines in a richtextbox.
Search for the string "Thomas".
It will give u an index where this starts in the textbox.
Use the Found=mid(string,foundindex,6) so that Found=Thomas, BUT if the downloaded text is from a unix box, this is not working.

Any ideas?
ASKER CERTIFIED SOLUTION
Avatar of Anthony Perkins
Anthony Perkins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you might also have to convert it to unicode.  I don't know what text format unix uses, but vb uses unicode.  what you do see on the unix stuff?  does it give an error?
I see the the text as a normal textbox.. can't see anything unusal. The problem is that for each time it finds what I am looking for, the value of a seems to increase too much (and it increases per time).

I thought this could be because I am going futher down into the file, so there are more "strange/invisibile" characters found and added. I don't think my mid function sees the same string as the richtextbox control does.
When I convert it to unicode, it adds a square between every character.

/Thomas