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 / August 2006

Tip: Looking for answers? Try searching our database.

Problem changing color of style in VBA

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
kbutterly@yahoo.com - 11 Aug 2006 16:20 GMT
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
Jean-Guy Marcil - 11 Aug 2006 16:50 GMT
kbutterly@yahoo.com was telling us:
kbutterly@yahoo.com nous racontait que :

> Good morning all,
>
[quoted text clipped - 27 lines]
> 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

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

Jay Freedman - 11 Aug 2006 17:05 GMT
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).

Signature

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.

> Good morning all,
>
[quoted text clipped - 30 lines]
> Thanks,
> Kathryn
Greg Maxey - 11 Aug 2006 18:14 GMT
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
> Good morning all,
>
[quoted text clipped - 30 lines]
> Thanks,
> Kathryn
kbutterly@yahoo.com - 11 Aug 2006 18:51 GMT
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

> Why are you cycling through the entire style collection?  Wouldn't this
> work as well:
[quoted text clipped - 50 lines]
> > Thanks,
> > Kathryn
 
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.