Solved

Accessing parts of text in text boxes (70pts)

Posted on 1998-12-11
3
131 Views
Last Modified: 2010-05-03
How can I read a specific word of a certain line in a text box? If I have a box containing lots of words, I want to be able to get a certain word and read it by referring to some kind of address, e.g.

If I had a box containing the sentence "The cat sat on the mat.", how could I ask the program to return, say, word 3?
0
Comment
Question by:The_Oracle
3 Comments
 

Expert Comment

by:lankford
ID: 1449588
As long as you are using a vb text box, you are probably going to have to parse the text and figure it out your self.

If you are programmatically using Microsoft Word as your text box, then you can actually refer to the third word in the Word object with something like this:

   ThirdWord = Document.Word(3)

Does anyone out there know of a text box ocx control that stores its information in easily accessible objects like this?

Lankford

0
 
LVL 1

Expert Comment

by:OeilNoir
ID: 1449589
Yep, you would have to check all text, but you could write a function who would create a kind of index of words. something what would keep the position of the word and it's lenth
then with the index you could get your word

"this is my string and i want to find a word in it"

for the word "my" you would keep the position (9) and the lenth (2) . and you could read your index to know where the words are. would be longer whe you create your index, but faster when you search your word...
0
 
LVL 8

Accepted Solution

by:
MikeP090797 earned 70 total points
ID: 1449590
Here are some parsing functions you might find useful to achive that task:

'PURPOSE:    Finds part of a string found after the N occurence of a substring
'ARGUMENTS:
'   IN szMainString:    string to extract from
'   IN szSubString:     substring to look for
'   IN iOccurence:      the number of the occurence to return the string after it
'   IN bCaseSensetive:  specifies whether the search is case sensetive
'RETURNS:    The string located after the first occurence of a substring
'
Public Function GetAfterSubstring(szMainString As String, szSubString As String, Optional iOccurence As Integer = 1, Optional bCaseSensetive As Boolean = False) As String
    Dim iPos As Integer
    iPos = InstrN(szMainString, szSubString, iOccurence, bCaseSensetive)
   
    'Return "" if substring is not found, or if there is nothing after it
    If iPos = 0 Or iPos = Len(szMainString) Then
        GetAfterSubstring = ""
        Exit Function
    End If
   
    GetAfterSubstring = Right(szMainString, Len(szMainString) - iPos)
End Function
'PURPOSE:    Finds part of a string found before the N occurence of a substring
'ARGUMENTS:
'   IN szMainString:    string to extract from
'   IN szSubString:     substring to look for
'   IN iOccurence:      the number of the occurence to return the string before it
'   IN bCaseSensetive:  specifies whether the search is case sensetive
'RETURNS:    The string located before the first occurence of a substring
'
Public Function GetBeforeSubstring(szMainString As String, szSubString As String, Optional iOccurence As Integer = 1, Optional bCaseSensetive As Boolean = False) As String
    Dim iPos As Integer
    iPos = InstrN(szMainString, szSubString, iOccurence, bCaseSensetive)
   
    'Return "" if substring is not found
    If iPos = 0 Then
        GetBeforeSubstring = ""
        Exit Function
    End If
   
    GetBeforeSubstring = Left(szMainString, iPos - 1)
End Function
'PURPOSE:   Locates the position of the last occurence of a substring in a string
'ARGUMENTS:
'   IN szMainString:    string to search
'   IN szSubString:     substring to look for
'   IN bCaseSensetive   Specifies wheteher the string comparison method is case sensetive
'RETURNS:   The position of the last substring in a string
'
Public Function InstrL(szMainString As String, szSubString As String, Optional bCaseSensetive As Boolean = False) As Integer
    Dim I As Integer
    Dim nLen As Integer
    Dim nLen1 As Integer
    Dim iComparisonMethod As Integer
    Dim bFlag As Boolean
   
    nLen = Len(szMainString)
    nLen1 = Len(szSubString)
    If nLen = 0 Or nLen1 = 0 Then Exit Function
   
    'Set comparison method
    iComparisonMethod = IIf(bCaseSensetive, 0, 1)
   
    'Look for the first substring from the end
    For I = nLen - nLen1 To 1 Step -1
        If InStr(I, szMainString, szSubString, iComparisonMethod) > 0 Then
            bFlag = True
            Exit For
        End If
    Next I
   
    If Not bFlag Then
        InstrL = 0
        Exit Function
    End If
   
    InstrL = I
End Function
'PURPOSE:   Locates the position of the N occurence of a substring in a string
'ARGUMENTS:
'   IN szMainString:    string to search
'   IN szSubString:     substring to look for
'   IN iOccurrance:     the number of the occurence of the substring to return
'   IN bCaseSensetive   Specifies wheteher the string comparison method is case sensetive
'RETURNS:   The position of the N substring in a string
'
Public Function InstrN(szMainString As String, szSubString As String, Optional iOccurance As Integer = 1, Optional bCaseSensetive As Boolean = False) As Integer
    Dim I As Integer
    Dim iComparisonMethod As Integer
    Dim iPos As Integer
   
    iComparisonMethod = IIf(bCaseSensetive, 0, 1)
    iPos = 1
   
    If iOccurance = 1 Then
        InstrN = InStr(1, szMainString, szSubString, iComparisonMethod)
        Exit Function
    End If
   
   
    For I = 1 To iOccurance 'Look for the substring N times
        iPos = InStr(iPos + 1, szMainString, szSubString, iComparisonMethod)
        If iPos = 0 Then    'If no more found, means that there are less occurences then specified
            InstrN = 0
            Exit Function
        End If
    Next I
   
    InstrN = iPos
End Function

'PURPOSE:   Counts how many times a substring appears in a string
'ARGUMENTS:
'   IN szMainString:    string to look in
'   IN szSubString:     string to look for
'   IN bCaseSensetive:  Specifies whether the comparison is case sensetive
'RETURNS:   The number of times the substring occures in the string
'
Public Function GetOccurrenceCount(szMainString As String, szSubString As String, Optional bCaseSensetive As Boolean = False) As Long
    Dim iPos As Integer
    Dim iCount As Integer
    Dim iComparisonMethod As Integer
   
    iComparisonMethod = IIf(bCaseSensetive, 0, 1)   'Set comparison method
    iPos = InStr(1, szMainString, szSubString, iComparisonMethod)
   
    While iPos > 0      'Loop while finding substring
        iCount = iCount + 1
        iPos = InStr(iPos + 1, szMainString, szSubString, iComparisonMethod)
    Wend
   
    GetOccurrenceCount = iCount
End Function

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

919 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

22 Experts available now in Live!

Get 1:1 Help Now