Greetings.
I have some code that is going in a contiunious loop.
I want to find some text
Replace it with a hyperlink
Then change the display name of the link
But what happens is that if the display name is the same as the link i want
to find it keeps going around in circles.
If there is something that can be done to it i would really appreciated it .
Thanks
Sub FindAndReplaceWithHypertext()
Dim rngReplace As Word.Range
Dim rngFound As Word.Range
Dim szFindTerm As String
Dim szReplaceHL As String
Dim szTexttoDP As String
szFindTerm = InputBox("Enter the word you wish to replace with a
hyperlink:")
szReplaceHL = InputBox("Enter Adobe File Name:")
szTexttoDP = InputBox("Enter HyperLink Display Name")
Set rngReplace = ActiveDocument.Content
With rngReplace.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = szFindTerm
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
' Find all occurrences in the document
Do While .Execute
' Create and use a totally independent range object
Set rngFound = rngReplace.Duplicate
' Replace sought text with hyperlink
rngFound.Text = vbNullString
ActiveDocument.Hyperlinks.Add rngFound, szReplaceHL, _
TextToDisplay:=szTexttoDP
' Resume the search after the text that we just found
rngReplace.Collapse wdCollapseEnd
Loop
End With
End Sub
Hi Casey,
I can't remember that I ever needed a duplicate of a range.
Seems to me, it overcomplicates things.
Can't write an essay on how to handle ranges,
right here and now.
But have a look at the line I marked.
I changed variables name to what I am used to.
Sub FindAndReplaceWithHypertext()
Dim rRpl As Range ' range replace
Dim rFnd As Range ' range found
Dim sFnd As String ' string to be found
Dim sHpl As String ' hyperlink
Dim sDsp As String ' hyperlink display
sFnd = "August"
sHpl = "c:\test\test.doc"
sDsp = "August"
Set rRpl = ActiveDocument.Range
ResetSearch
With rRpl.Find
.Text = sFnd
.Wrap = wdFindStop
Do While .Execute
Set rFnd = rRpl.Duplicate
rFnd.Text = vbNullString
ActiveDocument.Hyperlinks.Add _
rFnd, sHpl, TextToDisplay:=sDsp
rRpl.Select ' <<<<<<<<<<<<<<<<<<<<<<<
rRpl.Collapse wdCollapseEnd
Loop
End With
ResetSearch
End Sub
And this is the way I would do it:
Sub FindAndReplaceWithHypertextImproved()
Dim rDoc As Range ' range replace
Dim sFnd As String ' string to be found
Dim sHpl As String ' hyperlink
Dim sDsp As String ' hyperlink display
sFnd = "August"
sHpl = "c:\test\test.doc"
sDsp = "August"
Set rDoc = ActiveDocument.Range
ResetSearch
With rDoc.Find
.Text = sFnd
.Wrap = wdFindStop
Do While .Execute
ActiveDocument.Hyperlinks.Add _
rDoc, sHpl, TextToDisplay:=sDsp
rDoc.End = rDoc.End + Len(sDsp) ' !!!
rDoc.Collapse wdCollapseEnd ' !!!
' be the master of the range
Loop
End With
End Sub
' ---
Public Sub ResetSearch()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
End Sub
HTH

Signature
Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
Casey Mac - 05 Oct 2005 12:52 GMT
Thankyou Helmut!
That is perfect! Im just learning VBA and after looking at your code i can
see that my "hack" job was a total mess.
Thanks again
Cheers
PS i love the web!
> Hi Casey,
>
[quoted text clipped - 82 lines]
>
> HTH