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

(e-mail address removed) was telling us:
(e-mail address removed) 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
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
K

koryklein

(e-mail address removed) was telling us:
(e-mail address removed) 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
(e-mail address removed)
Word MVP site:http://www.word.mvps.org

That did it!! Thanks!
 

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