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 / January 2007

Tip: Looking for answers? Try searching our database.

Macro to Change Text Color

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Thomas M - 02 Jan 2007 08:29 GMT
Word 2003

When I am in the process of developing a lengthy document I tend to
color code the text so that I can keep track of issues that need to be
addressed before the document is completed.  I use the following colors
in my scheme:

    Red = Stuff that needs to be fixed because its wrong or imcomplete
    Orange = Stuff that is being researched
    Green = Stuff that is completed and confirmed as being correct
    Black = New text (The color is black as opposed to automatic)
    Aqua = Text that will be in red when the document is finished
        (Special notes, tips, warnings, etc.)

When I finish a red or orange section I turn it green so that I know the
section is done.  Likewise for new text.  Nonetheless, when I show the
paragraph markers I see a lot of red and orange mixed in the document.  
What I would like is a macro that goes through the document paragraph-
by-paragraph and does the following (in this order of prescendence):

    1) If the paragraph is a blank line turn the text to automatic
    2) If the paragraph color is aqua turn the text to red
    3) For any other paragraph color turn the text to automatic

Can someone show me how this would be done in VBA?

--Tom
Lene Fredborg - 02 Jan 2007 09:32 GMT
The macro below will do what you want. Please note that the paragraph color
is determined by checking the color of the first character in the paragraph
only.

Sub ChangeFontColor()

   Dim oPara As Paragraph
   
   For Each oPara In ActiveDocument.Paragraphs
       With oPara.Range
           'If blank line: color automatic
           If .Text = Chr(13) Then
               .Font.Color = wdColorAutomatic
           Else
               'If aqua: color red
               If .Characters(1).Font.Color = wdColorAqua Then
                   .Font.Color = wdColorRed
               'Else: color automatic
               Else
                   .Font.Color = wdColorAutomatic
               End If
           End If
       End With
   Next oPara
   MsgBox "Finished changing colors."
End Sub

NOTE: You should always make sure to include the paragraph mark in the
selection when you change the font color (the reason why paragraph marks are
a different color than the text is that they have not been selected when you
changed the color). Have you considered creating a paragraph style for each
of the colors you use?

Signature

Regards
Lene Fredborg
DocTools – Denmark
www.thedoctools.com
Document automation – add-ins, macros and templates for Microsoft Word

> Word 2003
>
[quoted text clipped - 23 lines]
>
> --Tom
Helmut Weber - 02 Jan 2007 10:07 GMT
Hi Lene, hi Thomas,

at second thought I think the whole thing is ill conceived.
It needs more than just changing colors.
What if Thomas runs our macros twice?

If track changes has to be avoided for some reason,
one must utilize some additional font property
like some fancy underlining, to prevent
all font colors to be reset to automatic
with a second run of the macros.

Unless Thomas takes the risk...

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Lene Fredborg - 02 Jan 2007 10:38 GMT
Hi Helmut and Thomas,

I agree with you, Helmut. The macro solutions do what Thomas requested but
the result may not be as desired if the macros are run under the wrong
conditions.

Signature

Regards
Lene Fredborg
DocTools – Denmark
www.thedoctools.com
Document automation – add-ins, macros and templates for Microsoft Word

> Hi Lene, hi Thomas,
>
[quoted text clipped - 9 lines]
>
> Unless Thomas takes the risk...
Thomas M. - 02 Jan 2007 17:42 GMT
I would agree that I need to come up with a better way of working with Word
in this respect.  Fortunately, while my current document is fairly lengthy
(about 50 pages) the formatting is pretty straight forward so the macros
that you and Helmut have posted should work in this particular case.  The
only drawback is the hyperlinks, but there aren't all that many of those so
I could do those manually if need be.

I do a lot of documenting for my employer so I think this kind of thing will
be a recurring need in the future, which is why I'm trying to find a better
way of working with Word.  In the past I've developed document sections in
separate files and then copied them into the main document only when they
are completed, but then you have to keep track of a bunch of different
files, which can present problems also.  This document is my first attempt
at keeping everything in one file (backed up nightly) and still having some
method of tracking which sections are done and which sections need more
work.

Anyway, the help you and Helmut have provided should get me through on this
document, and you've also given me some ideas of better ways to do things.
Thanks for the help!

--Tom

> Hi Helmut and Thomas,
>
[quoted text clipped - 15 lines]
>>
>> Unless Thomas takes the risk...
Thomas M. - 03 Jan 2007 19:20 GMT
Just thought I'd follow-up with an FYI.  I have been experimenting with
using styles to color code my text and that seems to work really well.  I
haven't done a lot with it yet, but I've done a few tests and it seems to be
a good option.

I setup a style that does nothing but change the font color.  When the style
is applied to a paragraph it has no impact on the hyperlinks, and Track
Changes (which I really don't have to worry about since I'm the only editor
of the documents and don't need to worry about tracking changes) also does
not seem to have an impact other than the fact that you need to accept style
change.  Things like bold, italics, and underline also don't matter because
the custom style does not act on those parameters.  I setup the custom style
so that the "Style for following paragraph" parameter is set to Normal,
which eliminates the problem of having blank lines that have a custom
format.

The only issue that I have found is that the custom style will reset the
line spacing if the line spacing of the current paragraph does not match the
line spacing setting of the style.  I can't see any way around that problem
because there is no way to configure the style so that it has no impact on
line spacing--you have to provide a line spacing value.  I don't use
anything other than single spacing, so it's immaterial to me, but that could
be an issue for others so I thought that I'd mention it.

Finally, using styles makes it a snap to reformat paragraphs.  All you have
to do is display the Styles and Formatting bar, click on the down arrow next
to the custom style and select the first option (Select all x instances) to
select every paragraph using that style, and then click another style to
change the style of all selected paragraphs.

It may not be the perfect solution, but it's far superior to using macros to
change the font color, which is the way that I've been doing it.

--Tom

> Hi Helmut and Thomas,
>
[quoted text clipped - 15 lines]
>>
>> Unless Thomas takes the risk...
Thomas M - 02 Jan 2007 10:48 GMT
> The macro below will do what you want. Please note that the paragraph color
> is determined by checking the color of the first character in the paragraph
[quoted text clipped - 28 lines]
> changed the color). Have you considered creating a paragraph style for each
> of the colors you use?

I always try to select the paragraph mark when I change colors.  I think
that what happens is that sometimes multiple paragraphs (to provide
extra white space as a visible separation) get inserted when I am
working on a document, and then later when I take out excess white space
the paragraph markers that are left for the blank lines between
paragraphs may be a different color than the text on the screen.  When I
turn a paragraph a different color I try to remember to highlight the
white space too, but human nature being what it is, you're just never
going to catch them all.

The system I'm using at the moment is something that evolved in a piece-
meal fashion and without a lot of thought.  It was a quick and dirty
solution to an immediate need.  I have thought about better ways to work
with Word in this respect, including paragraph styles, but I just
haven't had time to pursue any other ideas.

--Tom
Helmut Weber - 02 Jan 2007 11:10 GMT
Hi Thomas,

>The system I'm using at the moment is something that evolved in a piece-
>meal fashion and without a lot of thought.  It was a quick and dirty
>solution to an immediate need.  I have thought about better ways to work
>with Word in this respect, including paragraph styles, but I just
>haven't had time to pursue any other ideas.

yes, that's the way it goes, which will help you only
in so far as you know that everybody has to decide
whether creating better tools is more efficient
or just muddling through.
With my last project, converting Web-pages to Word
with lots of mathematical formulas
and adjusting german to english text and vice versa
in respect to number of paragraphs and styles,
when I thought the tools were close to perfect,
the project was cancelled.

C'est la vie.

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Thomas M. - 02 Jan 2007 17:52 GMT
Yeah, there's always that trade-off between the time it takes to develop the
tools and the time that those same tools will save.  Sometimes the
development time is not worth the effort.  In my particular case I will be
doing a fair amount of documenting for my employer, so it will probably pay
off for me in the long run to develop some tools that will aid in the
development of documentation.  I think the best place for me to start is
with a re-evaluation of the way I am currently doing things so that I can
hopefully come up with a better system that will simplify the issue.

--Tom

> Hi Thomas,
>
[quoted text clipped - 16 lines]
>
> C'est la vie.
Helmut Weber - 02 Jan 2007 09:36 GMT
Hi Thomas,

like this, if I understand the problem correctly:

Sub MyColor()
Dim rngDcm As Range
Dim objPrg As Paragraph
Set rngDcm = ActiveDocument.Range
For Each objPrg In rngDcm.Paragraphs
  If Len(objPrg.Range) = 1 Then
     objPrg.Range.Font.Color = wdColorAutomatic
  ElseIf objPrg.Range.Font.Color = wdColorAqua Then
     objPrg.Range.Font.Color = wdColorRed
  Else
     objPrg.Range.Font.Color = wdColorAutomatic
  End If
Next
End Sub

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Thomas M - 02 Jan 2007 10:34 GMT
> Hi Thomas,
>
[quoted text clipped - 14 lines]
> Next
> End Sub

Thanks for the reply.  It looks like you understood my question
correctly.  I'll give your code a try.

However, there is one thing that I forgot to mention in my original
post, and that's the fact that the document contains a lot of
hyperlinks, and I would like the hyperlinks to stay the default color of
blue.  I was just browsing the VBA help in the hopes that there is a
Hyperlinks collection that a person could use to find all the hyperlinks
in the document and act on them in some way, but I don't see anything
like that.  The only other thing I could find to help with this is that
if you go to Edit > Find you can search for the hyperlink style and find
all the hyperlinks that way.

So after your code there could be a loop that goes through the document
finding all the hyperlinks and turning them blue.  It's been ages since
I worked with a Do loop, so I'm not really sure how to set up the loop
so that it would exit at the proper time to avoid entering an infinite
loop.  I suppose that I could capture the current position (CPos) of the
cursor and compare it to the previous position (PPos).  As long as CPos
> PPos it means that the cursor is moving down through the document.  
Once CPos < PPos it would mean that the cursor has returned to the top
of the document, at which point you could exit the loop.  There might be
an easier way, but that's the method that comes to mind at the moment.

--Tom
Helmut Weber - 02 Jan 2007 10:46 GMT
Hi Thomas,

like this:

Dim objHpr As Hyperlink
For Each objHpr In ActiveDocument.Hyperlinks
  objHpr.Range.Style = "Hyperlink"
Next

But see my reply to Lene.
With some exceptions more,
the thing will get pretty complicated.

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

 
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.