Re: how can i check in a sentence a specific word to see if exists
Hi Salsa,
To my eyes, this is sort of the classic use of Instr(). However, there are a few things to consider, depending on exactly what you're wanting. For instance, the case (upper or lower), as well as whether the word being a part of a larger word is acceptable.
The case issue is easily solved with LCase$() or UCase$(). But the "within larger word" issue is a bit trickier. If it were me, I'd possibly add a space to the beginning and ending of the sentence, and then search for " going " (notice prefix and suffix spaces). Adding the spaces to the sentence allows you to find your " going " string even if it's at the start or end of the sentence.
Good Luck,
Elroy
EDIT1: Also, if there are embedded vbTab or vbCrLf type characters in your sentence, that's another problem. You could use Replace$() to replace all of that kind of stuff you think you'll run across. Punctuation (period, comma, semi-colon) are other possible problems.
Last edited by Elroy; Dec 12th, 2017 at 09:41 AM.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
Re: how can i check in a sentence a specific word to see if exists
You can get by without using LCase$() or UCase$() by adding the optional parameters to INSTR()
You can also get around the whole word by doing two checks one with a leading space and one with a trailing space to cover instances where the word would be the first or last word in the sentence though that would fail if it is the only word in the sentence.
Code:
Dim searchin As String
searchin = "i need to find if going exists in the sentence ."
If InStr(1, searchin, " going", vbTextCompare) Or InStr(1, searchin, "going ", vbTextCompare) Then
MsgBox "is there"
Else
MsgBox "not there"
End If
Re: how can i check in a sentence a specific word to see if exists
salsa
yes, easy stuff (basically)
Put a RichTextBox on a new form, name it rtb1. Put a text box on the form and set its text value to 'aid'
add a command button
put this code in the form, and then run it and click the command button
Code:
Option Explicit
Private Sub Command1_Click()
Dim mWord As String
Dim mSentence As String
mWord = " " & UCase(Text1.Text) & " "
mSentence = " " & UCase(rtb1.Text) & " "
Dim i As Integer
i = InStr(1, mSentence, mWord)
If i > 0 Then
MsgBox "Your search for '" & Text1.Text & "' in '" & rtb1.Text & "' was successful!"
Else
MsgBox "Sorry...no matches."
End If
End Sub
Private Sub Form_Load()
rtb1.Text = "Now is the time for all good men to come to the aid of their party."
End Sub
Now, why I used a richtextbox is because if you want to SEE that word found, say, in RED, the RTB is a great component to use for doing that. I did NOT write that code, but it is easy to do...if you want to do it, look up the RTB .textrtf and how to display some of it in colors
Re: how can i check in a sentence a specific word to see if exists
TOM is helpful for this.
Code:
Option Explicit
Private Const WM_USER As Long = &H400&
Private Const EM_GETOLEINTERFACE As Long = WM_USER + 60
Private Declare Function SendMessage Lib "user32" Alias "SendMessageW" ( _
ByVal hWnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
Private tdRTB As tom.ITextDocument
Private Sub Command_Click()
Dim Found As Boolean
With tdRTB.Range(0, -1)
Do While .FindText(Text.Text, tomForward, tomMatchWord)
Found = True
.Font.BackColor = vbYellow
.MoveStart 1, tomWord
Loop
End With
Label.Caption = CStr(Found)
End Sub
Private Sub Form_Load()
Dim IUnknown As stdole.IUnknown
SendMessage RTB.hWnd, EM_GETOLEINTERFACE, 0, VarPtr(IUnknown)
Set tdRTB = IUnknown
End Sub
Private Sub Form_Resize()
If WindowState <> vbMinimized Then
With Frame
.Move 0, ScaleHeight - .Height, ScaleWidth
RTB.Move 0, 0, ScaleWidth, .Top
End With
End If
End Sub
Re: how can i check in a sentence a specific word to see if exists
Originally Posted by dilettante
TOM is helpful for this.
Code:
Option Explicit
Private Const WM_USER As Long = &H400&
Private Const EM_GETOLEINTERFACE As Long = WM_USER + 60
Private Declare Function SendMessage Lib "user32" Alias "SendMessageW" ( _
ByVal hWnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
Private tdRTB As tom.ITextDocument
Private Sub Command_Click()
Dim Found As Boolean
With tdRTB.Range(0, -1)
Do While .FindText(Text.Text, tomForward, tomMatchWord)
Found = True
.Font.BackColor = vbYellow
.MoveStart 1, tomWord
Loop
End With
Label.Caption = CStr(Found)
End Sub
Private Sub Form_Load()
Dim IUnknown As stdole.IUnknown
SendMessage RTB.hWnd, EM_GETOLEINTERFACE, 0, VarPtr(IUnknown)
Set tdRTB = IUnknown
End Sub
Private Sub Form_Resize()
If WindowState <> vbMinimized Then
With Frame
.Move 0, ScaleHeight - .Height, ScaleWidth
RTB.Move 0, 0, ScaleWidth, .Top
End With
End If
End Sub
great !!
regards
Chris
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
Re: [RESOLVED] how can i check in a sentence a specific word to see if exists
Look up ITextDocument in your MSDN Library CD online docs. If you have the October 2001 version installed (and maybe some earlier ones) the TOM documentation is there.
A little incomplete, and slightly wrong in a few places (some TOM methods used to have optional parameters but haven't in a very long time). More current info can be found at the MSDN web site but most of the VB6 examples have been removed.
RichEdit controls and Word both implement TOM as an object model, though some things only work in Word.
Re: [RESOLVED] how can i check in a sentence a specific word to see if exists
Hi dilettante,
I really don't know why I missed ITextDocument all these years, just shows you
that you can still learn something , evan a old Horse like me.
regards
Chris
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
Here is a simple regexp "library" designed to emulate PHP's preg functions sort of, with a sample usage and a somewhat complicated test-case (punctuation as first boundary, end-of-string as second)
vb Code:
Option Explicit
Private Sub Form_Load()
Dim sText As String
Dim sWord As String
Dim vIndexes As Variant
sText = "salsa is going to shop.going"
sWord = "going"
If preg_match("\b" & preg_escape(sWord) & "\b", sText, Indexes:=vIndexes) > 0 Then
Debug.Print "Found at positions: " & Join(vIndexes, ", ")
Re: [RESOLVED] how can i check in a sentence a specific word to see if exists
Hi,
might as well put my simple way in here...
Code:
Private Sub Command1_Click()
Dim SearchMethod As Boolean
Dim i As Long
SearchMethod = CBool(Check1.Value * -1)
i = FindString(RichTextBox1, Text1.Text, , SearchMethod)
Print i
RichTextBox1.SetFocus
End Sub
Private Sub Form_Load()
Check1.Caption = "SearchForward"
Check1.Value = 1
RichTextBox1.Text = "salsa is going to shop,salsa is Going to shop"
RichTextBox1.HideSelection = False
Text1.Text = "going"
End Sub
'-----------------------------------------------
'suchen in einer RTF-Box, optional mit Markieren
'-----------------------------------------------
Public Function FindString(RTF As RichTextBox, _
sString As String, _
Optional CompareAs As VbCompareMethod = _
vbTextCompare, _
Optional SearchForward _
As Boolean = True, _
Optional SelText As _
Boolean = True) As Long
Dim i As Long
'suchen vorwärts
If SearchForward Then
i = RTF.SelStart + RTF.SelLength + 1
i = InStr(i, RTF.Text, sString, CompareAs)
'suchen rückwärts
Else
i = RTF.SelStart - RTF.SelLength
i = InStrRev(RTF.Text, sString, i, CompareAs)
End If
'gefunden an Position (0=nicht gefunden)
FindString = i
'markieren gefundenen Text
If SelText Then
With RTF
'wenn gefunden
If i > 0 Then
.SelStart = i - 1
.SelLength = Len(Text1.Text)
'wenn nicht gefunden
Else
'Cursor an Textende
If SearchForward Then
.SelStart = Len(.Text)
.SelLength = 0
'Cursor an Textanfang
Else
.SelStart = 0
.SelLength = 0
End If
End If
End With
End If
End Function
regards
Chris
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.