Problem with font color macro

M

Mark F.

I have the following macro that is supposed to change all the keywords
in a Word document to blue. This macro however does not work. Any help
would be appreciated.

Thanks!

'
========================================================================
==
Sub Colorize()
'
' Colorize Macro
'
========================================================================
==

Dim sKeywords, i As Integer

sKeywords = Array( _
"Alias", "As", "ByRef", "ByVal", "Const", "Declare", "Dim", _
"Do", "Double", "Each", "Else", "End", "For", "Function", _
"If", "Integer", "Lib", "Long", "Loop", "New", "Next", _
"Private", "Public", "Short", "Sub", "Then", "Until", _
"Wend", "While")

For i = LBound(sKeywords) To UBound(sKeywords)

Selection.Find.ClearFormatting
With Selection.Find
.Text = sKeywords(i)
.Replacement.Text = sKeywords(i)
.Forward = True
.Font.Color = wdColorBlue
.Wrap = wdFindContinue
.Format = False
.MatchCase = True
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Next i

End Sub
' ====== end =========================================================
 
J

Jean-Guy Marcil

Hi Mark,

Try this instead:

Sub Colorize()
'
' Colorize Macro
'==========================================================================

Dim sKeywords, i As Integer

sKeywords = Array( _
"Alias", "As", "ByRef", "ByVal", "Const", "Declare", "Dim", _
"Do", "Double", "Each", "Else", "End", "For", "Function", _
"If", "Integer", "Lib", "Long", "Loop", "New", "Next", _
"Private", "Public", "Short", "Sub", "Then", "Until", _
"Wend", "While")

For i = LBound(sKeywords) To UBound(sKeywords)

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Font.Color = wdColorBlue
With Selection.Find
.Text = sKeywords(i)
.Replacement.Text = sKeywords(i)
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = True
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Next i

End Sub
' ====== end =========================================================

Or, to make the code shorter:

Sub Colorize()
'
' Colorize Macro
'==========================================================================

Dim sKeywords, i As Integer

sKeywords = Array( _
"Alias", "As", "ByRef", "ByVal", "Const", "Declare", "Dim", _
"Do", "Double", "Each", "Else", "End", "For", "Function", _
"If", "Integer", "Lib", "Long", "Loop", "New", "Next", _
"Private", "Public", "Short", "Sub", "Then", "Until", _
"Wend", "While")

For i = LBound(sKeywords) To UBound(sKeywords)

With ActiveDocument.Content.Find
.ClearFormatting
With .Replacement
.ClearFormatting
.Font.Color = wdColorBlue
End With
.Execute FindText:=sKeywords(i), ReplaceWith:=sKeywords(i), _
Format:=True, MatchCase:=True, MatchWholeWord:=True, _
Replace:=wdReplaceAll
End With


Next i

End Sub
' ====== end =========================================================

Since you want to change formatting, do not forget to set Format to True!

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

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