|
-
Nov 30th, 2003, 09:40 AM
#1
Thread Starter
Junior Member
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
-
Dec 3rd, 2003, 09:36 PM
#2
Fanatic Member
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:
Sub Test1()
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"
With acTable.Cell(r, 2).Range
For i = 0 To 2
With .Find
.Text = mystring(i)
.Forward = True
.Wrap = wdFindContinue
.Execute
End With
If .Find.Found Then
msg = MsgBox("Add " & mystring(i), vbOKCancel)
If msg = vbOK Then
acTable.Cell(r, 4).Range.InsertAfter mystring(i)
End If
End If
Next i
End With
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|