Results 1 to 2 of 2

Thread: Searching for text in Word table

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2001
    Posts
    29

    Searching for text in Word table

    Dear all

    I try to search for certain text in a specified cell of a Word table, then fill the found text into another cell in this table by click a button. My code:

    Dim acTable As Table
    Dim mystring(0 To 2) As String
    Dim r As Integer
    Dim i As Integer
    Dim msg As String
    Dim rng As Range

    Set acTable = ThisDocument.Tables(2)

    r = acTable.Rows.Count

    mystring(0) = "Pilot"
    mystring(1) = "ATCo"
    mystring(2) = "Aircraft"


    Set rng = acTable.Cell(r, 2).Range


    For i = 0 To 6

    With rng.Find

    .Text = mystring(i)
    .Forward = True
    .Wrap = wdFindStop
    End With

    rng.Find.Execute

    If rng.Find.Found Then

    msg = MsgBox("Add " & mystring(i), vbOKCancel)

    If msg = vbOK Then

    With acTable
    .Cell(r, 4).Range.InsertAfter mystring(i)

    End With
    End If

    End If

    Next i


    In the code, mystring(i) is predefined text which I want to find in a cell of Word table and fill them into other cells of this tabel.

    So far, this code works ok if I only search once, or search one text or word in specified cell, however, some times, I need to search two different text or words in the cell, I have to set the Wrap property as

    .Wrap=wdFindContinue

    instead of .Wrap=wdFindStop

    but this setting will search other part of the document as well, this is not what I want.

    For example:

    the cell content: The pilot requests pushback from the ATCo

    the text I want to find from this content: pilot and ATCo

    However, the text Aircraft is not in the cell but in other table of the Word document will be found, this is not what I want. I only want to search in a specified cell.

    I appreciate any ideas and help

  2. #2
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591
    When you Execute the Find method on a Range object, the Range object is set to the results of the Find. So rng is getting set everytime you do rng.Find.Execute. You can correct this by putting the line "Set rng = acTable.Cell(r, 2).Range" as the first line inside the For...Next loop so that rng gets reset to the cell every time. Or, you can use use With acTable.Cell(r, 2).Range and use wdFindContinue to keep searching that range (because there is no longer a Range object variable that would need to be reset).

    VB Code:
    1. Sub Test1()
    2.    
    3.     Dim acTable As Table
    4.     Dim mystring(0 To 2) As String
    5.     Dim r As Integer
    6.     Dim i As Integer
    7.     Dim msg As String
    8.     Dim rng As Range
    9.    
    10.     Set acTable = ThisDocument.Tables(2)
    11.    
    12.     r = acTable.Rows.Count
    13.    
    14.     mystring(0) = "Pilot"
    15.     mystring(1) = "ATCo"
    16.     mystring(2) = "Aircraft"
    17.    
    18.     With acTable.Cell(r, 2).Range
    19.    
    20.         For i = 0 To 2
    21.        
    22.             With .Find
    23.                 .Text = mystring(i)
    24.                 .Forward = True
    25.                 .Wrap = wdFindContinue
    26.                 .Execute
    27.             End With
    28.        
    29.             If .Find.Found Then
    30.                
    31.                 msg = MsgBox("Add " & mystring(i), vbOKCancel)
    32.                
    33.                 If msg = vbOK Then
    34.                     acTable.Cell(r, 4).Range.InsertAfter mystring(i)
    35.                 End If
    36.        
    37.             End If
    38.        
    39.         Next i
    40.  
    41.     End With
    42.  
    43. End Sub

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