Problem changing color of style in VBA

K

kbutterly

Good morning all,

I have some code that I want to use to color code text based on the
style name. The code runs fine, but font color does not change for one
of the styles. Here is the code:

For Each st In ActiveDocument.Styles
If InStr(st.NameLocal, "Agency") > 0 Then
st.Font.Color = wdColorBrightGreen
st.Font.Hidden = False
ElseIf InStr(st.NameLocal, "PO") > 0 Then
st.Font.Color = wdColorBrightGreen
st.Font.Hidden = False
End If
Next

The code changes the color of the text with style 'Body Text Agency' to
bright green, but does not change the color of the text with style
'Body Text PO'.

Both styles are paragragh styles. They are both based on style 'Body
Text'

When I step through the code, st.Font.Color changes from -16777216 to
65280 for both styles. If I go to Paragraph Styles, 'Body Text PO'
shows that it is formatted as bright green. However, while 'Body Text
Agency' appears bright green, 'Body Text PO' still appears black on the
screen.

Am I missing somethign obvious here? Any ideas, references, or
resources would be greatly appreciated.

Thanks,
Kathryn
 
J

Jean-Guy Marcil

(e-mail address removed) was telling us:
(e-mail address removed) nous racontait que :
Good morning all,

I have some code that I want to use to color code text based on the
style name. The code runs fine, but font color does not change for
one of the styles. Here is the code:

For Each st In ActiveDocument.Styles
If InStr(st.NameLocal, "Agency") > 0 Then
st.Font.Color = wdColorBrightGreen
st.Font.Hidden = False
ElseIf InStr(st.NameLocal, "PO") > 0 Then
st.Font.Color = wdColorBrightGreen
st.Font.Hidden = False
End If
Next

The code changes the color of the text with style 'Body Text Agency'
to bright green, but does not change the color of the text with style
'Body Text PO'.

Both styles are paragragh styles. They are both based on style 'Body
Text'

When I step through the code, st.Font.Color changes from -16777216 to
65280 for both styles. If I go to Paragraph Styles, 'Body Text PO'
shows that it is formatted as bright green. However, while 'Body Text
Agency' appears bright green, 'Body Text PO' still appears black on
the screen.

Am I missing somethign obvious here? Any ideas, references, or
resources would be greatly appreciated.

If text based on a style was manually altered on the screen, then changing
the style definition will not automatically alter the manual alterations.

So, if you manually changed the colour of the PO text in the document
changing the colour definitions of the PO style will not have an impact on
the PO text in the document.

You may need to reset the font formatting of the concerned paragraphs.

.Range.Font.Reset
or, if you want to reset all style attribute:
.Range.ParagraphFormat.Reset

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

Jay Freedman

It probably isn't anything to do with your code. Is it possible that the
text with Body Text PO style in your test document has had the black color
applied manually, overriding the style's color? If so, modifying the style's
color won't change the text's appearance until you reset the text to the
default. You do that manually by selecting the text and pressing
Ctrl+spacebar, or in code by Selection.Font.Reset (or the equivalent for a
range).

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
G

Greg Maxey

Why are you cycling through the entire style collection? Wouldn't this
work as well:

Sub Test()
ActiveDocument.Range.Font.Reset
On Error GoTo Handler
With ActiveDocument
With .Styles("Agency").Font
.Color = wdColorBrightGreen
.Hidden = False
End With
With .Styles("PO").Font
.Color = wdColorBrightGreen
.Hidden = False
End With
End With
Exit Sub
Handler:
Resume Next
End Sub
 
K

kbutterly

Greg,

Good question: I am working on a training manual, which has two
audiences, Agency people and Program Office people. Right now, I only
have the two different styles based on audience, but in the future I
might have more.

I think I will use your code, though, to simplify things right now.

Thanks,
Kathryn
 

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