font color line based on character search

R

robert.hatcher

I write a little VBA for excel and sometimes I want to print out the
code and have comment lines print in their green color. However
printing from VBA results in no color :(. I paste into word and print
from there but the font color doesnt transfer. I was trying to write
code that looks for the ' selects the rest of the line and changes the
color, cycling through the entire document. Im having two problems

1. I can get the the code to find the ', it seems to find stuff at
random
2. I need help selecting "to the end of the line" I am selecting the
entire line with:
(.Expand Unit:=wdLine

thanks in advance
Robert

Sub ColorVBA()

Dim i

For Each i In ActiveDocument.Characters

i = "'"
Selection.Find.ClearFormatting
With Selection.Find
.Text = i
'.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute

With Selection
.Expand Unit:=wdLine
.Font.Color = wdColorGreen

End With

Next i


End Sub
 
J

Jean-Guy Marcil

(e-mail address removed) was telling us:
(e-mail address removed) nous racontait que :
I write a little VBA for excel and sometimes I want to print out the
code and have comment lines print in their green color. However
printing from VBA results in no color :(. I paste into word and print
from there but the font color doesnt transfer. I was trying to write
code that looks for the ' selects the rest of the line and changes the
color, cycling through the entire document. Im having two problems

1. I can get the the code to find the ', it seems to find stuff at
random
2. I need help selecting "to the end of the line" I am selecting the
entire line with:
(.Expand Unit:=wdLine

thanks in advance

Try this:

Sub Test()
Dim rgeDoc As Range
Dim rgeFound As Range

Set rgeDoc = ActiveDocument.Range

With rgeDoc.Find
.Text = "'"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
Do While .Execute
.Parent.Select
Set rgeFound = Selection.Bookmarks("\Line").Range
With rgeFound
'The If block is to ignore apostrophes within the code (inside
strings)
'The ' can be rendered in many diffrent ways, and if you
manually
'modified the code in the doc you may need all three versions,
'if not, 39 should be enough
If AscW(rgeFound.Characters(1)) = 8216 Or _
AscW(rgeFound.Characters(1)) = 49 Or _
AscW(rgeFound.Characters(1)) = 39 Then
.Font.Color = wdColorGreen
End If
Set rgeDoc = ActiveDocument.Range(rgeFound.End, _
ActiveDocument.Range.End)
End With
Loop
End With

End Sub


--

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

robert.hatcher

Thanks Jean-Guy, for the code for the excelent comments which I found
very informative!
 

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