Troubleshoot a VBA macro to change color

I

ivanov.ivaylo

Hi all,

I try to adapt a macro to my needs but an error appears. Can you help
me with it?

The macro should search for words and phrases from a list which are
formatted in italic and clolorize them in green and the italic should
be preserved.


Sub Colorize()

Dim sKeywords, i As Integer
sKeywords = Array("adj", "adv", "attr", "aux", "cj", "comp",
"demonstr", "ger", "imp", "impers", "inf", "int", "n", "num", "part",
"pers", "pl", "poss", "pp", "predic", "pref", "prep", "pres p", "pron",
"pt", "refl", "sing", "sl", "o.s.", "o.'s" "v")
For i = LBound(sKeywords) To UBound(sKeywords)
With ActiveDocument.Content.Find
.ClearFormatting
With .Replacement
.ClearFormatting
.Font.Color = wdColorGreen
.Font.Italic = True
End With
.Execute FindText:=sKeywords(i), ReplaceWith:=sKeywords(i), _
Format:=True, MatchCase:=True, Italic:=True,
MatchWholeWord:=True, _
Replace:=wdReplaceAll
End With
Next i
End Sub
 
G

Greg

Sub Colorize()
Dim sKeywords As Variant
Dim i As Long
sKeywords = Array("adj", "adv", "attr", "aux", "cj", "comp", _
"demonstr", "ger", "imp", "impers", "inf", "int", "n", "num", "part", _
"pers", "pl", "poss", "pp", "predic", "pref", "prep", "pres p", "pron",
_
"pt", "refl", "sing", "sl", "o.s.", "o.'s", "v")
For i = LBound(sKeywords) To UBound(sKeywords)
With ActiveDocument.Content.Find
.ClearFormatting
.Text = sKeywords(i)
.Font.Italic = True
.Format = True
.MatchWholeWord = True
.MatchCase = True
With .Replacement
.ClearFormatting
.Font.Color = wdColorGreen
End With
.Execute Replace:=wdReplaceAll
End With
Next i
End Sub
 
I

ivanov.ivaylo

Hi Jonathan,

I fixed this. The VBA Editor tries to debug: Italic:=True in

..Execute FindText:=sKeywords(i), ReplaceWith:=sKeywords(i),
Format:=True, MatchCase:=True, Italic:=True, MatchWholeWord:=True,
Replace:=wdReplaceAll
 
G

Greg

Ivan,

Some I suppose prefer this format:

..Execute FindText:=sKeywords(i), ReplaceWith:=sKeywords(i),
Format:=True, MatchCase:=True, Italic:=True, MatchWholeWord:=True,
Replace:=wdReplaceAll

I find it confusing and like to lay it out line for line.
 
J

Jonathan West

Greg said:
Ivan,

Some I suppose prefer this format:

.Execute FindText:=sKeywords(i), ReplaceWith:=sKeywords(i),
Format:=True, MatchCase:=True, Italic:=True, MatchWholeWord:=True,
Replace:=wdReplaceAll

I find it confusing and like to lay it out line for line.

You have to check the help file. Italic is not one of the parameters of the
Execute method of the Find object, so you cannot put it there.

You can only specify Italic as one of the Font properties of the Find or
Replacement objects.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
G

Greg

Jonathan,

<Italic is not one of the parameters of the Execute method of the Find
object, so you cannot put it <here.

Yes I knew that and probably shouldn't have cut and pasted Mr. Ivanov's
code in stating that I don't like clumping alot of things together on
the .execute line.
 

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

Similar Threads


Top