Need Macro Advice

D

douglascfast

Hello all,

Need some advice / help

I need a macro in Word that will look for a sting /* and then find
another Sting */ and turn those strings and what is between them
Green. I would use the same function for single quotes and turn what
is between them red.

I have the Keyword bit down thanks to some postings on the web.

I'm trying to color code my SQL code and my VBA code for print outs
(helps me read what is going on.

Any help would be great.

Thanks

Doug
 
M

m rafala

How about this:

Sub MakeGreen()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Font.ColorIndex = wdBrightGreen
With Selection.Find
.Text = "/*[!\/]/"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
 
D

douglascfast

What can I say?

EXCELLENT!

Here is what I did with it, for the three options.
1) /* */ Green comment
2) -- Green comment
3) 'aaa' Red for literal's

Any ideas to make it better / faster / easier = or something I missed?

Love to learn and maybe someone else can use as well . . . .

Sub SQLComment()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
' Selection.Find.Replacement.Font.Color = wdColorGreen

With Selection.Find
.Text = "/*[!\/]*/"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Replacement.Font.Color = wdColorGreen
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With

Selection.Find.Execute Replace:=wdReplaceAll

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting

With Selection.Find
.Text = "'[!\/]'"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Replacement.Font.Color = wdColorRed
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With

Selection.Find.Execute Replace:=wdReplaceAll

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting

With Selection.Find
.Text = "--**^13"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Replacement.Font.Color = wdColorBlue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With

Selection.Find.Execute Replace:=wdReplaceAll
End Sub
 
M

m rafala

Hmmm...

What I typed is not quite what I intended.

Looks like I typed this: .Text = "/*[!\/]/"
It's better off like this: .Text = "/\*[!\/]@\*/"

which means essentially, find a slash, asterisk, a string of non-slashes,
asterisk, followed by a slash. The former works, but its not as precise (and
kind of pointless).
 

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