Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / August 2007

Tip: Looking for answers? Try searching our database.

How to Add Hyperlinks with Search Function?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
koryklein@gmail.com - 09 Aug 2007 01:15 GMT
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!
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.