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

Tip: Looking for answers? Try searching our database.

Troubleshoot a VBA macro to change color

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ivanov.ivaylo@gmail.com - 04 Apr 2006 12:37 GMT
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
Jonathan West - 04 Apr 2006 12:46 GMT
Hi ivanov,

You're missing a comma here

   "o.'s" "v")

should be

   "o.'s", "v")

Signature

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

> Hi all,
>
[quoted text clipped - 27 lines]
>    Next i
> End Sub
ivanov.ivaylo@gmail.com - 04 Apr 2006 13:04 GMT
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
Greg - 04 Apr 2006 13:00 GMT
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
ivanov.ivaylo@gmail.com - 04 Apr 2006 13:18 GMT
thanks greg!

that's it.

ivaylo
Greg - 04 Apr 2006 13:26 GMT
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.
Jonathan West - 04 Apr 2006 13:42 GMT
> Ivan,
>
[quoted text clipped - 5 lines]
>
> 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.

Signature

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 

Greg - 04 Apr 2006 15:36 GMT
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.
 
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.