How to find, select text in table and change its colour ?

P

Peter

Hello

I have one table with words (YES) or (NO). I'd like to write VBA code which
could find, select and change colour to red of (YES) words in table.
Could you help me to write VBA code with it ?
I'm looking forward to your replies.

Regards

Peter
 
P

Peter

I dit it as you wrote. I record new macro with search, select and change
colour to red of my (YES) word in the table, but next time I run it it
nothing happens. I have the VBA code:
Sub nowe()
'
' new Makro
' Makro saved 2004-07-19 by Peter
'
Selection.Find.ClearFormatting
With Selection.Find
.Text = "TAK"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Font.Color = wdColorRed
Selection.Font.Bold = wdToggle
End Sub

How can it be run automatically after I fill the form in which I choose
words (YES) and (NO) ?
Where am I to put this code (in my form or in a module ?) ?

King regards

Peter
 
H

Helmut Weber

Hi Peter,
if you got only 1 table, then this table is table(1).
These are 2 macros. Put them in a module.
Sub Test454()
Dim oTbl As Table
Set oTbl = ActiveDocument.Tables(1)
ResetSearch
With oTbl.Range.Find
.Text = "yes"
.Replacement.Text = "yes"
.Replacement.Font.Color = wdColorRed
.MatchWholeWord = True
.Execute Replace:=wdReplaceAll
End With
ResetSearch
End Sub
---
Sub ResetSearch()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
End Sub
---
Let us know, if you need further help.
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000
 
P

Peter

Also I was considering what can be change in this formula to select all the
row of the table (in which there is although one word YES) and change color
of all words in this row to red.
Have you got any idea ?
Regards
Peter
 
H

Helmut Weber

Hi Peter,
Also I was considering what can be change
in this formula to select all the
row of the table (in which there is although
one word YES) and change color of all words to red.
That is somewhat different. I am assuming,
you want all words in a row to be red,
if "yes" is found in that row.
Sub Test453()
Dim oRow As Row ' object row
Dim oTbl As Table ' object table
Set oTbl = ActiveDocument.Tables(1)
ResetSearch ' sub resetsearch
For Each oRow In oTbl.Range.Rows
With oRow.Range.Find
.Text = "yes"
.MatchWholeWord = True
If .Execute Then
oRow.Range.Font.Color = wdColorRed
End If
End With
Next
ResetSearch ' sub resetsearch
End Sub
Strictly speaking, this method changes the color
of the row.range to red, not just the words in that
range. And with columns it would be quite different,
as there is no column.range.
 
P

Peter

And one more question this searches one table but if I'd like to search all
the tables in my document (I've got 9 ones) what should be change in this
VBA formula ?
Thanks for your help
Peter
 
H

Helmut Weber

Hi Peter,
Sub Test450()
Dim oRow As Row ' object row
Dim oTbl As Table ' object table
ResetSearch
For Each oTbl In ActiveDocument.Tables
For Each oRow In oTbl.Range.Rows
With oRow.Range.Find
.Text = "yes"
.MatchWholeWord = True
If .Execute Then
oRow.Range.Font.Color = wdColorRed
End If
End With
Next
Next
ResetSearch
End Sub
 

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