Hello,
I am fairly new to VBA programming and am having trouble figuring out
an error. The following code searches through a word document
highlighting certain words with are being passed to this function. In
addition to the highlighting, I want to add a hyperlink to each word
that is highlighted. I can't seem to set the right anchor. I can't
figure out how to set the anchor on the search word. Thanks in
advance!
CODE:
Sub FindAndHighlight(ByVal rngStory As Word.Range, myYellowWord As
String, NewBackColor As WdColorIndex, NewForeColor As WdColorIndex,
ByVal matchWholeWord As Boolean, myReason As String)
Do Until (rngStory Is Nothing)
With rngStory.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = myYellowWord
.matchWholeWord = matchWholeWord
While .Execute
rngStory.Hyperlinks.Add Address:="http://www.msn.com/", _
Anchor:=rngSory, _
ScreenTip:=myReason
rngStory.HighlightColorIndex = NewBackColor 'wdYellow
rngStory.Font.ColorIndex = NewForeColor 'wdColorRed
Wend
End With
Set rngStory = rngStory.NextStoryRange
Loop
End Sub
Jean-Guy Marcil - 09 Aug 2007 03:11 GMT
koryklein@gmail.com was telling us:
koryklein@gmail.com nous racontait que :
> Hello,
>
[quoted text clipped - 5 lines]
> figure out how to set the anchor on the search word. Thanks in
> advance!
Try this:
'_______________________________________
Do Until (rngStory Is Nothing)
With rngStory.Find
.Text = myYellowWord
.MatchWholeWord = MatchWholeWord
.Forward = True
.Wrap = wdFindStop
Do While .Execute
.Parent.Hyperlinks.Add Address:="http://www.msn.com/", _
Anchor:=.Parent, _
ScreenTip:=myReason
With .Parent
.HighlightColorIndex = NewBackColor
.Font.ColorIndex = NewForeColor
'We need +1 or the next loop will pickup the _
word we just hyperlinked as part of the range _
because of the hyperlink that was just inserted, _
thus creating an infinite loop
rngStory.SetRange .End + 1, rngStory.StoryLength
End With
Loop
End With
Set rngStory = rngStory.NextStoryRange
Loop
'_______________________________________

Signature
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org
koryklein@gmail.com - 09 Aug 2007 17:36 GMT
> korykl...@gmail.com was telling us:
> korykl...@gmail.com nous racontait que :
[quoted text clipped - 44 lines]
> jmarcilREM...@CAPSsympatico.caTHISTOO
> Word MVP site:http://www.word.mvps.org
That did it!! Thanks!