Help with Macro to Change Font Color

J

James Walker

Hello,



I'm looking for a macro that will search a Word document (I have Word 2003)
for a given character (in this case an asterisk). Once it finds the
character, I would like for that character and all the characters to the
left of it, until the first space to be a specific color. There could be
zero asterisks to hundreds of asterisks within a single document.



As an example, if this was in my document: "This is a test* and only a
test." I would like test* to be a specific color.



Any help would be appreciated.

James Walker, Jr.
 
T

Tony Jollans

Using Find and Replace

Look for: [! *]{1,}\* - that's space, left (square) bracket, exclamation
mark, space, asterisk, right bracket, left brace, one, comma, right brace,
backslash, asterisk

Replace with (nothing) but set the Format > Font > Font Color to what you
want

Check Use Wildcards
Hit Replace All
 
D

Dawn Crosier

I think you should be able to modify this code to suit your
needs. What this code does is search through a body of text and
search for the key word "Select" and then it looks for the
keyword "Go" and colors all the text between the two instances.

Public Sub SearchAndReturnExample()



Dim rngOriginalSelection As Word.Range

Dim colFoundItems As New Collection

Dim rngCurrent As Word.Range

Dim strSearchFor As String

Dim intFindCounter As Integer



Set rngOriginalSelection = Selection.Range



'Find all the words "Select" in the document

'and add them to a collection.

With Selection.Find

.ClearFormatting

.Forward = True

.Wrap = wdFindContinue

.Text = "Select"

.MatchWholeWord = True

.Execute

Do While .Found = True

'Keep track of the number of instances found

intFindCounter = intFindCounter + 1

colFoundItems.Add Selection.Range,
CStr(intFindCounter)

.Execute

Loop

End With



rngOriginalSelection.Select



'Re-open the first range in the collection

'look for the word "Go"

'Extend the range

For Each rngCurrent In colFoundItems

rngCurrent.Select

Selection.Extend

With Selection.Find

.Text = "Go"

.Forward = True

End With

Selection.Find.Execute



'Format the range for Blue

Selection.Font.Color = wdColorBlue



'Format the range for Bold

Selection.Font.Bold = True



'Locate the next range to Repeat

Next rngCurrent



'Return to the beginning of the doc

rngOriginalSelection.Select



End Sub


--
Dawn Crosier
Microsoft MVP
"Education Lasts a Lifetime"

This message is posted to a newsgroup. Please post replies and
questions to the newsgroup so that others can learn as well.

Hello,



I'm looking for a macro that will search a Word document (I have
Word 2003)
for a given character (in this case an asterisk). Once it finds
the
character, I would like for that character and all the characters
to the
left of it, until the first space to be a specific color. There
could be
zero asterisks to hundreds of asterisks within a single document.



As an example, if this was in my document: "This is a test* and
only a
test." I would like test* to be a specific color.



Any help would be appreciated.

James Walker, Jr.
 
H

Helmut Weber

Hi everybody,

like Tony said (good wildcard pattern):

Sub test0987()
Selection.HomeKey unit:=wdStory
Selection.ExtendMode = False
ResetSearch
With Selection.Find
.Text = "[! *]{1,}\*"
.MatchWildcards = True
.Replacement.Font.Color = wdColorBlue
.Execute replace:=wdReplaceAll
End With
ResetSearch
End Sub

Public 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

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 

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