[RESOLVED] How to read certain words in a textbox
Hi, I'm trying to read text in a window, like an AOL window, and I want to be able to read and pick out certain words in the textbox. I'm able to read the whole sentence, but I can't pick out any words in the sentence. Here is my code:
VB Code:
' Find the window.
lHwnd = FindWindow(vbNullString, "vegric")
' Find the chat box.
lHwnd = FindWindowEx(lHwnd, ByVal 0&, "RichTextWndClass", vbNullString)
'allocate a 255 byte string; the first two bytes indicate the length
strBuff = Chr$(255) & String$(256, vbNullChar)
'now this gets the last line of the textbox
Call SendMessage(lHwnd, EM_GETLINE, SendMessage(lHwnd, EM_GETLINECOUNT, 0, ByVal 0&) - 2, ByVal StrPtr(strBuff))
'convert the string to Unicode
sWindowText = StrConv(strBuff, vbUnicode)
MsgBox sWindowText
'below instead of saying 'if sWindowText = "hello"', I want to say something like if sWindowText CONTAINS "hello"
If sWindowText = "hello" Then
MsgBox ("The word 'hello' is in this sentence")
End If
Thanks,
Brad
Re: How to read certain words in a textbox
VB Code:
If InStr(sWindowText, "hello") > 0 Then
'Do stuff
End If
Or if you don't want the case of the text to matter...
VB Code:
If InStr(LCase$(WindowText), "hello") > 0 Then
'Do stuff
End If
Re: How to read certain words in a textbox
If you want to see if a string contains certain text, you can use the Like operator, eg:
VB Code:
If sWindowText Like "*hello*" Then
..but that will find it within other words too (such as "shello" ?!).
Alternatively you can split the text into an array, and look at each item, eg:
VB Code:
Dim arrSplit() as String
Dim intCounter as Integer
arrSplit = Split(sWindowText, " ")
For intCounter = 0 To Ubound(arrSplit)
If arrSplit(intCounter) = "hello" Then
MsgBox ("The word 'hello' is in this sentence")
End If
Next intCounter
Re: How to read certain words in a textbox
Quote:
Originally Posted by si_the_geek
...but that will find it within other words too (such as "shello" ?!)
I think you might have been thinking of the musical instrument, the Chello :-)
Or maybe Hell On Earth but without the spaces? :-P
Should suggest he searches for "* hello *" with the spaces if he doesn't want the partial matches
Re: How to read certain words in a textbox
As yes, Chello would have been a much better example!
The problem with searching for spaces is that the word might be at the start/end of the string, which would need extra checking - the Split version covers the issue more cleanly.
Re: [RESOLVED] How to read certain words in a textbox
Thank you. I'm ok now. :)
Re: How to read certain words in a textbox
Quote:
Originally Posted by BrendanDavis
Or if you don't want the case of the text to matter...
VB Code:
If InStr(LCase$(WindowText), "hello") > 0 Then
'Do stuff
End If
Or
VB Code:
InStr(1, WindowText, "hello", vbTextCompare)
;)
Re: How to read certain words in a textbox
Quote:
Originally Posted by DigiRev
Or
VB Code:
InStr(1, WindowText, "hello", vbTextCompare)
;)
Ah, true. But mine is shorter! lol
:P
P.S. Why the hell would you want to split the entire thing by spaces and look up each word? That involves creating an unnecessary array, and it's a whole lot slower. If all you need to know is if a certain word exists within a given string, InStr will tell you if the word is in there quickly and easily. You could even replace "hello" with " hello " to avoid the whole "Hell On Earth w/o spaces" problem, lol.
P.P.S. Whoever corrected the "shello" mistake by stating that it was "chello," you should know that it's actually spelled "cello" and thus would not be a complication in this particular instance.
Re: [RESOLVED] How to read certain words in a textbox
InStr's Text Compare is slow compared to it's Binary Compare, so you'd probably be better off doing
VB Code:
InStr(LCase$(WindowText), "hello")
' rather than
InStr(1, WindowText, "hello", vbTextCompare)
Re: How to read certain words in a textbox
Quote:
Originally Posted by si_the_geek
As yes, Chello would have been a much better example!
The problem with searching for spaces is that the word might be at the start/end of the string, which would need extra checking - the Split version covers the issue more cleanly.
In a large string, that would be a whole lot of work for nothing, when all you'd need to do is check for " hello" or " hello ", et cetera. Two lines of quick-executing code Vs. several lines of slower code makes a BIG difference when you start getting into strings or text files larger than a few bytes.
Re: [RESOLVED] How to read certain words in a textbox
Quote:
Originally Posted by bushmobile
InStr's Text Compare is slow compared to it's Binary Compare, so you'd probably be better off doing
VB Code:
InStr(LCase$(WindowText), "hello")
' rather than
InStr(1, WindowText, "hello", vbTextCompare)
'Tis the way I've always done it. Just seems more "at home" to me :)
Re: How to read certain words in a textbox
Quote:
Originally Posted by BrendanDavis
P.P.S. Whoever corrected the "shello" mistake by stating that it was "chello," you should know that it's actually spelled "cello" and thus would not be a complication in this particular instance.
Yeah, I know...it's said "chello" but spelled cello :-)
I couldn't think of any word beginning or ending with "hello", the same problem si had but I was more inventive with my answer :-)
Not that it is important, but there IS a Chello out there...it's an ISP :-)
http://www.reference.com/browse/wiki/Chello ...not that I've actually heard of them (although the name seems familiar now)
Re: How to read certain words in a textbox
Quote:
Originally Posted by si_the_geek
The problem with searching for spaces is that the word might be at the start/end of the string, which would need extra checking - the Split version covers the issue more cleanly.
Simple enough to do. When "hello" is found in the string, the program sends the location to a function that confirms that it is a valid result and returns 1 if so, and 0 if it finds characters before or after that aren't spaces or aren't null (as in the beginning and end of the string)...or maybe another way that could check for beginning/end like the actual start point of the word or start+len equalling the length of the text etc. This isn't rocket science :-)
I would *like* to use the split way but it means checking each word individually...using instr alone and having a verification for the info would fix the problem :-P
Re: How to read certain words in a textbox
Much simpler would be to append spaces to the string to check. ;)
VB Code:
InStr(LCase$(" " & WindowText & " "), " hello ")
Quote:
Originally Posted by BrendanDavis
In a large string, that would be a whole lot of work for nothing, when all you'd need to do is check for " hello" or " hello ", et cetera. Two lines of quick-executing code Vs. several lines of slower code makes a BIG difference when you start getting into strings or text files larger than a few bytes.
Agreed, if just searching for one word then InStr would be faster, even with the extra checks.
However, for multiple words it is likely to be more efficient to use the array in combination with a Select Case.