How to apply color to upper case characters into selection of text?

A

avkokin

Hello. There is a certain text which contains some words with upper
case character (not only first words into the sentences). I want to
apply color to this upper case characters into selection of text . I
wrote the small code, but it runs only for first words of sentences
into selection of text:
Sub colorCase()
Dim fChar As Range
Dim selText As Range
Set selText = Selection.Range
If Selection.Type = wdSelectionIP Then
MsgBox "Don't selection of text"
Else
For Each fChar In selText.Sentences
fChar.Characters.First.Font.Color = wdColorRed
Next fChar
End If
End Sub
Please help me. Thank you very much.
 
J

Jay Freedman

First, you're getting only the first characters of sentences because
that's what you asked for in "For Each fChar In selText.Sentences". If
you want to check every word in the selection, you need to say "For
Each fChar In selText.Words".

Second, you didn't do anything to make sure you're recoloring only the
upper case letters -- after you start looking at words instead of
sentences, you would have seen that you're recoloring the first letter
of every word, regardless of its case. There are several ways to fix
this, but the easiest is an If statement that uses the Like operator,
as in this example:

Sub colorCase()
Dim fChar As Range
Dim selText As Range
Set selText = Selection.Range
If Selection.Type = wdSelectionIP Then
MsgBox "Don't selection of text"
Else
For Each fChar In selText.Words
If fChar.Characters.First Like "[A-Z]" Then
fChar.Characters.First.Font.Color = wdColorRed
End If
Next fChar
End If
End Sub
 
A

avkokin

First, you're getting only the first characters of sentences because
that's what you asked for in "For Each fChar In selText.Sentences". If
you want to check every word in the selection, you need to say "For
Each fChar In selText.Words".

Second, you didn't do anything to make sure you're recoloring only the
upper case letters -- after you start looking at words instead of
sentences, you would have seen that you're recoloring the first letter
of every word, regardless of its case. There are several ways to fix
this, but the easiest is an If statement that uses the Like operator,
as in this example:

Sub colorCase()
Dim fChar As Range
Dim selText As Range
Set selText = Selection.Range
If Selection.Type = wdSelectionIP Then
MsgBox "Don't selection of text"
Else
For Each fChar In selText.Words
If fChar.Characters.First Like "[A-Z]" Then
fChar.Characters.First.Font.Color = wdColorRed
End If
Next fChar
End If
End Sub

Hello. There is a certain text which contains some words with upper
case character (not only first words into the sentences). I want to
apply color to this upper case characters into selection of text . I
wrote the small code, but it runs only for first words of sentences
into selection of text:
Sub colorCase()
Dim fChar As Range
Dim selText As Range
Set selText = Selection.Range
If Selection.Type = wdSelectionIP Then
MsgBox "Don't selection of text"
Else
For Each fChar In selText.Sentences
fChar.Characters.First.Font.Color = wdColorRed
Next fChar
End If
End Sub
Please help me. Thank you very much.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ:http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.- óËÒÙÔØ ÃÉÔÉÒÕÅÍÙÊ ÔÅËÓÔ -

- ðÏËÁÚÁÔØ ÃÉÔÉÒÕÅÍÙÊ ÔÅËÓÔ -

Thank you, Jay! It's good work and rather!
 

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