Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / December 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
avkokin - 09 Dec 2007 18:10 GMT
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.
Jay Freedman - 09 Dec 2007 19:14 GMT
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
[quoted text clipped - 14 lines]
>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.
avkokin - 09 Dec 2007 19:43 GMT
> 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
[quoted text clipped - 49 lines]
>
> - Показать цитируемый текст -

Thank you, Jay! It's good work and rather!
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.