Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with macro that replaces line break and colors text

Status
Not open for further replies.

sonname

Programmer
May 18, 2001
115
US
I need help with writing a macro that scans through a document and for all instances of "hyphen, line break tokens", replace those and change the color of the word. For example, if I have the word pro-<[lb]>bably, I want to be able to remove the -<[lb]> from the word and then change the color font on the word "probably". In essence, the macro needs to be able to select the words on both sides of the hyphen/line break, bring them together and change the color. Here is a recorded macro that I did, but I know that I need to do it for a whole document.


Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
With Selection.Find
.Text = "-<[lb]>"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.MoveLeft Unit:=wdWord, Count:=1
Selection.MoveRight Unit:=wdWord, Count:=5, Extend:=wdExtend
With Selection.Font
.Name = ""
.Bold = False
.Italic = False
.Underline = wdUnderlineNone
.UnderlineColor = wdColorAutomatic
.StrikeThrough = False
.DoubleStrikeThrough = False
.Outline = False
.Emboss = False
.Shadow = False
.SmallCaps = False
.AllCaps = False
.Color = wdColorRed
.Engrave = False
.Superscript = False
.Subscript = False
.Spacing = 0
.Scaling = 100
.Position = 0
.Kerning = 0
.Animation = wdAnimationNone
End With
Selection.MoveRight Unit:=wdCharacter, Count:=1
 
I would use the words collection object and instr, and replace:
Code:
for each w in activedocument.words
   if instr(1,w,<your token>)>-1 then
     w=replace(w,<your token>,<your replacement>
   end if
next

_________________
Bob Rashkin
 
Forgot about the color
Code:
for each w in activedocument.words
   if instr(1,w,<your token>)>-1 then
     w=replace(w,<your token>,<your replacement>
     w.font.colorindex=3  'for, say, RED
   end if
next

_________________
Bob Rashkin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top