Help me with InsertBefore/InsertAfter

I

ivanov.ivaylo

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
 
E

Edward Thrashcort

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
 
E

Edward Thrashcort

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
 
J

Jay Freedman

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

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 

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