finding within an only table column

P

Pablo Cardellino

Hi,

I'm trying to perform a search within a certain column of a table.
Find.Execute must not match any word within the other table columns. But
this is right what happens. The Find find terms within the first column, but
find terms within de second column too.

First I'll paste the code (explained) that defines the range, which seems to
be the origin of the problem:

Set termosdic = dicDoc.Tables(1).Cell(2, 1).Range
'I begin from the second row to jump
' the header cell (which I can delete,
' if necessary)

termosdic.Select

Selection.MoveDown unit:=wdLine, Count:=dicDoc.Tables(1).Rows.Count - 2,
_
Extend:=wdExtend
' I didn't achieve to do this withous
' the Selection object

Selection.Font.Color = wdColorRed
' this colors the first column cells,
' except for the Cell(1,1)

Set termosdic = Selection.Range
termosdic.Font.Color = wdColorBlue
' this colors almost all the table,
' from the Cell(2,1) up to the last
' cell of the first column, *including*
' the cells of the other columns,
' except the ones of the last row (just
' as if I'd select using the mouse)

termosdic.Select
' just for testing purpouses: this
' selects the same thas was previously
' selected, despite the previous
' coloring action





And this is the search, which should match just terms from the first column:

With loctermosdic.Find
.Text = LCase(termo.Text)
.MatchWildcards = False
.MatchWholeWord = True
.Wrap = wdFindStop

termoachado = False

Do: While .Execute
If .Found Then
Set ch = loctermosdic.Duplicate
ch.SetRange Start:=ch.Start - 1, End:=ch.Start
If ch.Text <> "-" Then
ch.SetRange Start:=loctermosdic.End,
End:=loctermosdic.End + 1
If ch.Text <> "-" Then
termoachado = True
loctermosdic.Select
Exit Do
End If
End If
End If
Wend: Exit Do: Loop
End With


Any help will be most appreciated

Thanks in advance,
Pablo
 
G

Greg Maxey

AFAIK, you can't define a column as search range explicitly. What you can
do is search each cell in a specific column only:

Sub ScratchMacro()
Dim oRng As Word.Range
Dim oCell As Word.Cell
'Find and replace in column 3 only.
For Each oCell In ActiveDocument.Tables(1).Columns(3).Cells
Set oRng = oCell.Range
With oRng.Find
.Text = "Text to find"
.Replacement.Text = "Replacement Text"
.Execute Replace:=wdReplaceAll
End With
Next oCell
End Sub
 
P

Pablo Cardellino

Oh... that's not good...

each cell a word... the application will slow too much... I think I will
store all the texts of the first column into an external range, and perform
the searches within this range, using the paragraph number to identify de
respective table row...

Thanks, Greg

Regards,

Pablo
 
M

Manfred F

Hi Pablo,

:
...
you can do this via the selection object. Try the following code from the
macro recorder:

Selection.SelectColumn
Selection.Find.ClearFormatting
With Selection.Find
.Text = "test"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Find.Execute
Selection.Find.Execute

The trick is to use .SelectColumn. This gives you access to the column
"range". As always, when you (have to) use the Selection object, you have to
restore it afterwards..

Regards,
Manfred
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top