How to Add Hyperlinks with Search Function?

K

koryklein

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
 
J

Jean-Guy Marcil

[email protected] was telling us:
[email protected] nous racontait que :
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!

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
'_______________________________________


--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
[email protected]
Word MVP site: http://www.word.mvps.org
 
K

koryklein

[email protected] was telling us:
[email protected] nous racontait que :



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
'_______________________________________

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
[email protected]
Word MVP site:http://www.word.mvps.org

That did it!! Thanks!
 
Top