Results 1 to 11 of 11

Thread: (SelText GURUS) Find a word in a text box

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2000
    Location
    Atlanta, GA
    Posts
    177

    Unhappy (SelText GURUS) Find a word in a text box

    Hello, does any one know how to find a word in a text box and record the location without the use of any input/dialog boxes? I want to perform all functions directly in the code. Thanks in advance.

    FORM CODE BELOW:

    Private Sub Command1_Click()
    Dim szFindString As String ' initial string to find
    Dim hCmdBtn As Long ' handle of 'Find Next' command button
    Dim strArr() As Byte ' for API use
    Dim i As Integer ' position indicator in the loop

    gHTxtWnd = rtbDisplay.hwnd

    ' Fill in the structure.
    szFindString = "Code (if applicable)"

    ReDim strArr(0 To Len(szFindString) - 1)

    For i = 1 To Len(szFindString)
    strArr(i - 1) = Asc(Mid(szFindString, i, 1))
    Next i

    frText.flags = FR_MATCHCASE Or FR_NOUPDOWN Or FR_NOWHOLEWORD
    frText.lpfnHook = 0&
    frText.lpTemplateName = 0&
    frText.lStructSize = Len(frText)
    frText.hwndOwner = Me.hwnd
    frText.hInstance = App.hInstance
    frText.lpstrFindWhat = VarPtr(strArr(0))
    frText.lpstrReplaceWith = 0&
    frText.wFindWhatLen = Len(szFindString)
    frText.wReplaceWithLen = 0
    frText.lCustData = 0

    ' Show the dialog box.
    gHDlg = FindText(frText)

    ' Get the handle of the dialog box
    hCmdBtn = GetDlgItem(gHDlg, 1)

    ' Get necessary value for calling default window procedure.
    gOldDlgWndHandle = GetWindowLong(hCmdBtn, GWL_WNDPROC)

    If SetWindowLong(hCmdBtn, GWL_WNDPROC, AddressOf FindTextHookProc) = 0 Then
    gOldDlgWndHandle = 0
    End If

    End Sub

    MODULE CODE BELOW:
    Public Type FINDREPLACE
    lStructSize As Long ' size of this struct 0x20
    hwndOwner As Long ' handle to owner's window
    hInstance As Long ' instance handle of.EXE that
    ' contains cust. dlg. template
    flags As Long ' one or more of the FR_??
    lpstrFindWhat As Long ' ptr. to search string
    lpstrReplaceWith As Long ' ptr. to replace string
    wFindWhatLen As Integer ' size of find buffer
    wReplaceWithLen As Integer ' size of replace buffer
    lCustData As Long ' data passed to hook fn.
    lpfnHook As Long ' ptr. to hook fn. or NULL
    lpTemplateName As Long ' custom template name
    End Type

    Public Declare Function FindText Lib "comdlg32.dll" Alias "FindTextA" (pFindreplace As FINDREPLACE) As Long
    Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
    Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Public Declare Function GetDlgItem Lib "user32" (ByVal hDlg As Long, ByVal nIDDlgItem As Long) As Long
    Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Public Declare Function SetFocus Lib "user32" (ByVal hwnd As Long) As Long
    Public Declare Function IsDlgButtonChecked Lib "user32" (ByVal hDlg As Long, ByVal nIDButton As Long) As Long
    Public Declare Function GetDlgItemText Lib "user32" Alias "GetDlgItemTextA" (ByVal hDlg As Long, ByVal nIDDlgItem As Long, ByVal lpString As String, ByVal nMaxCount As Long) As Long

    Public Const GWL_WNDPROC = (-4)
    Public Const WM_LBUTTONDOWN = &H201

    Public Const FR_NOMATCHCASE = &H800
    Public Const FR_MATCHCASE = &H4
    Public Const FR_NOUPDOWN = &H400
    Public Const FR_UPDOWN = &H1
    Public Const FR_NOWHOLEWORD = &H1000
    Public Const FR_WHOLEWORD = &H2
    Public Const EM_SETSEL = &HB1

    Public Const MaxPatternLen = 50 ' Maximum Pattern Length

    Global gOldDlgWndHandle As Long
    Global frText As FINDREPLACE
    Global gTxtSrc As String
    Global gHDlg As Long
    Global gHTxtWnd As Long

    Function FindTextHookProc(ByVal hDlg As Long, ByVal uMsg As Long, _
    ByVal wParam As Long, ByVal lParam As Long) As Long

    Dim strPtn As String ' pattern string
    Dim hTxtBox As Long ' handle of the text box in dialog box
    Dim ptnLen As Integer ' actual length read by GetWindowString
    Dim sp As Integer ' start point of matching string
    Dim ep As Integer ' end point of matchiing string
    Dim ret As Long ' return value for SendMessage

    strPtn = Space(MaxPatternLen)

    Select Case uMsg
    Case WM_LBUTTONDOWN
    ' Get the pattern string
    ptnLen = GetDlgItemText(gHDlg, &H480, strPtn, MaxPatternLen)

    ' Call default window procedure
    If gOldDlgWndHandle <> 0 Then
    FindTextHookProc = CallWindowProc(gOldDlgWndHandle, _
    hDlg, uMsg, wParam, lParam)
    End If

    ' Customize the winodw procedure
    If ptnLen <> 0 Then
    strPtn = Left(strPtn, ptnLen)
    SetFocus gHTxtWnd

    ' Get the MatchCase option
    If IsDlgButtonChecked(gHDlg, &H411) = 0 Then
    sp = InStr(LCase(gTxtSrc), LCase(strPtn))
    Else
    sp = InStr(gTxtSrc, strPtn)
    End If

    sp = IIf(sp = 0, -1, sp - 1)

    If sp = -1 Then
    Call MessageNoFound
    Else
    MsgBox "Begin Code Extraction?"
    End If

    ep = Len(strPtn)
    ret = SendMessage(gHTxtWnd, EM_SETSEL, sp, sp + ep)
    End If

    Case Else
    ' Call the default window procedure
    If gOldDlgWndHandle <> 0 Then
    FindTextHookProc = CallWindowProc(gOldDlgWndHandle, _
    hDlg, uMsg, wParam, lParam)
    End If
    End Select
    End Function

    Sub MessageNoFound()
    MsgBox "No matches found"
    End Sub
    212 will lead you to the truth

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390
    TextinTextbox = "Hello there. This is the Text"

    Text2Find = "the"

    Position = Instr(TextinTextbox,Text2Find)

    :highight it?:

    TextBox.Setfocus

    TextBox.Selstart = Position
    TextBox.Sellength = Len(Text2Find)


    Like that?

    (you may have to add 1 to Position...I cant test at the moment)
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3
    Frenzied Member Microbasic's Avatar
    Join Date
    Mar 2001
    Posts
    1,402
    To find a word/string in a textbox:

    VB Code:
    1. Pos = [color=000000]InStr[/color](1, Text1.Text, "Word To Find", vbTextCompare)


    MicroBasic
    Dragon Shadow Trainer

    There is no good or evil in the world...only programmers and fools .

  4. #4
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390
    Wait...are you Searching a Textbox in another app?
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  5. #5
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    Just Wondering,

    No Input Boxes? Then either:

    A) You Want a static search for a specific word that never varies

    or

    B) You want an alternate way of inputting a search_for string

    or

    C) You want the source code that builds telepathic programs and controls.


    If C, I'm sorry, but I think Bill Gates has the copyright on that.

    Otherwise

    Please respond with A or B in the appropriate Input Box

    -It's Friday, What can I Say?
    -Lou

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Sep 2000
    Location
    Atlanta, GA
    Posts
    177

    Talking

    lol "A" It's Friday. I feel you.

    I haven't tested the coding that You guys have posted yet, but I will be searching a word document for certain user entered information. I'm using three static labels (ex. SSN as hotspots/flags (that's why I need to record the postion). Once the app hits the "SSN:" it will record the next 25 characters.
    212 will lead you to the truth

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Sep 2000
    Location
    Atlanta, GA
    Posts
    177
    any ideas?
    212 will lead you to the truth

  8. #8
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    Just a little more info:

    1) You are accessing your word document thru the Microsoft
    Word Object Library and its Methods, Right?

    2) I'm using three static labels : Are These just text strings in the word Document? If Not, What are they?

    Also, could you attach a sample of your word document for a little
    expoloratory programmic surgury?

    - Holding My Breath in Anticipation,
    - Lou

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Sep 2000
    Location
    Atlanta, GA
    Posts
    177
    Sorry I have been out with a cold for a few days.

    1) Yes I'm using the word object

    2) Yes they are text srings in the document, and I don't have a document that you could actually use to test. The document itself is in a table. I'm thinking about just trying to access the cell. Not sure how to identify the cell though.
    212 will lead you to the truth

  10. #10
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390
    well to put info into a cell i know its:

    objectname.ActiveDocument.Tables(1).Cell(1, 1).Range = "hello"

    to read from it:

    Variable = objectname.ActiveDocument.Tables(1).Cell(1, 1).Range

    reference the table number..(starts at 1) and cells are

    cell(Row,Column)

    Ok?
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  11. #11
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    geoff_xrx is right!

    But I attached a sample prog to illustrate the basics of cell
    inspection accessing word thru vb.

    -Hope this helps
    -Lou

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width