PDA

View Full Version : Find a word and add link to it



misticmoon
03-02-2005, 11:35 AM
Hi,

I want to search for a word in a document, select all the instances of that word and link them to the top of the document. I am trying this:

With ActiveDocument.Content.Find
.ClearFormatting
.MatchWholeWord = True
.Execute FindText:="Go To Top"
If .Found = True Then
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, _
SubAddress:="Top", TextToDisplay:= "Selection.Text"
End If
End With
This is not working because I think is just selecting the first instance.

Thanks.

misticmoon:rofl

Howard Kaikow
03-02-2005, 02:24 PM
You need to put your code in a loop.

Steve Roman's book Writing Word Macros has a useful explanation of using the Find object.

Also, see http://www.standards.com/index.html?WordVBABooks.

Anne Troy
03-04-2005, 05:54 PM
I have Roman's book right here in front of me...his "useful explanation of using the Find object" is nothing more than how to manipulate the Find Object...that's it. It isn't so great...

Howard Kaikow
03-05-2005, 07:54 AM
I have Roman's book right here in front of me...his "useful explanation of using the Find object" is nothing more than how to manipulate the Find Object...that's it. It isn't so great...

the book has a chapter on the find/replace objects.
very useful.

Anne Troy
03-05-2005, 08:26 AM
mistic: I assume you need an answer on this, and don't have time to learn about objects. That's why this forum is here. I am getting this code for you. Hang tight.

And here you go. Courtesy of smozgur (http://www.vbaexpress.com/forum/member.php?u=16):


Sub CreateGoToTop()
Dim strFindText As String

Application.ScreenUpdating = False
'Text to find
strFindText = "Go To Top"
'Goto top of the page first
Selection.HomeKey Unit:=wdStory

'Settings for find action
With Selection.Find
.ClearFormatting
.MatchWholeWord = True
.Forward = True
.Wrap = wdFindContinue
'First execution
.Execute FindText:=strFindText
'Go ahead until there is no more found
Do While .Found
'Create hyperlink
'No need TextToDisplay since you just want to add hyperlink
'but not change the text to be shown
If Selection.Hyperlinks.Count = 0 Then
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:="", _
SubAddress:="_top", ScreenTip:=""
End If
'Execute for next find
.Execute FindText:=strFindText
Loop
End With
'Goto top of the page again
Selection.HomeKey Unit:=wdStory

Application.ScreenUpdating = True

End Sub