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 / March 2007

Tip: Looking for answers? Try searching our database.

Help me with InsertBefore/InsertAfter

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ivanov.ivaylo@gmail.com - 30 Mar 2007 18:39 GMT
Greetings,

Can someone help me modify the macro below?

I have a list of words which must be tagged like this: [c]XXXX[/c]

I have a macro that I used a while ago which served me to colorize
these words in green. Can you help me modify this macro or use another
that looks for a word in the list and puts the tags [c] and [/c]
around it, then looks for the next word in the list, etc.

Hwre is my old macro:

Sub Colorize()

Dim sKeywords As Variant
Dim i As Long

Dim sKeywords As Variant
Dim i As Long
sKeywords = Array("abbr", "adj", "adv", "am", "attr", "aux", "cj",
"comp", "demonstr", _
"ger", "imp", "impers", "indef. art.", "inf", "int", "inter", "lat",
"n", "num", _
"part", "pers", "pl", "poss", "pp", "predic", "pref", "prep", "pres
p", "pron", "pt", "refl", "sing", "sl", "suf", "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

Thank you very much!
   End With
   .Execute Replace:=wdReplaceAll
 End With
Next i

End Sub
Edward Thrashcort - 30 Mar 2007 18:55 GMT
Try this

Sub AddTags(tag)

Dim sKeywords As Variant
Dim i As Long

Dim sKeywords As Variant
Dim i As Long

sKeywords = Array("abbr", "adj", "adv", "am", "attr", "aux", "cj", "comp",
"demonstr", _
"ger", "imp", "impers", "indef. art.", "inf", "int", "inter", "lat", "n",
"num", _
"part", "pers", "pl", "poss", "pp", "predic", "pref", "prep", "presp",
"pron", "pt", "refl", "sing", "sl", "suf", "v")

For i = LBound(sKeywords) To UBound(sKeywords)
 With ActiveDocument.Content.Find
   .ClearFormatting
   .Font.Italic = True
   .Format = True
   .MatchWholeWord = True
   .MatchCase = True
   .Text = sKeywords(i)

   With .Replacement
     .ClearFormatting
     .Font.Color = wdColorGreen
   End With

   While .Execute
       Selection.InsertBefore Text:="<" + tag + ">"
       Selection.InsertAfter Text:="</" + tag + ">"
   Wend
 End With
Next i

End Sub

Eddie
Edward Thrashcort - 30 Mar 2007 19:07 GMT
Sorry I didn't test my previous answer (it does not work!)

Here is one solution that does

Sub test()
  AddTags ("tag")
End Sub

Sub AddTags(tag)

Dim sKeywords As Variant
Dim i As Long

sKeywords = Array("abbr", "adj", "adv", "am", "attr", "aux", "cj", "comp",
"demonstr", _
"ger", "imp", "impers", "indef. art.", "inf", "int", "inter", "lat", "n",
"num", _
"part", "pers", "pl", "poss", "pp", "predic", "pref", "prep", "presp",
"pron", "pt", "refl", "sing", "sl", "suf", "v")

Selection.HomeKey unit:=wdStory

For i = LBound(sKeywords) To UBound(sKeywords)
 With Selection.Find
   .ClearFormatting
   .Format = True
   .MatchCase = True
   .Text = sKeywords(i)

   With .Replacement
     .ClearFormatting
     .Font.Color = wdColorGreen
   End With
   .Execute
   While .Found
       With Selection
           .InsertBefore Text:="<" + tag + ">"
           .InsertAfter Text:="</" + tag + ">"
           .Collapse direction:=wdCollapseEnd
       End With
       .Execute
   Wend
 End With
Next i

End Sub

Eddie
Jay Freedman - 30 Mar 2007 20:43 GMT
This is another method, using wildcard searching
(http://www.word.mvps.org/FAQs/General/UsingWildcards.htm). On a very long
document, it may run noticeably faster than stopping at each occurrence.

Sub Colorize()

Dim oRg As Range

Dim sKeywords As Variant
Dim i As Long
sKeywords = Array("abbr", "adj", "adv", "am", "attr", "aux", "cj", "comp",
"demonstr", _
"ger", "imp", "impers", "indef. art.", "inf", "int", "inter", "lat", "n",
"num", _
"part", "pers", "pl", "poss", "pp", "predic", "pref", "prep", "presp",
"pron", "pt", "refl", "sing", "sl", "suf", "v")

For i = LBound(sKeywords) To UBound(sKeywords)
 Set oRg = ActiveDocument.Range
 With oRg.Find
   .ClearFormatting
   .Text = "(<" & sKeywords(i)
   If Right(sKeywords(i), 1) = "." Then
       .Text = .Text & ")"
   Else
       .Text = .Text & ">)"
   End If
   .Font.Italic = True
   .Format = True
   .MatchWildcards = True
   With .Replacement
     .ClearFormatting
     .Font.Color = wdColorGreen
     .Text = "[c]\1[/c]"
   End With
   .Execute Replace:=wdReplaceAll
 End With
Next i

End Sub

Signature

Regards,
Jay Freedman
Microsoft Word MVP        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.

> Sorry I didn't test my previous answer (it does not work!)
>
[quoted text clipped - 44 lines]
>
> Eddie

Rate this thread:






 
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.