replace text with a hyperlink

H

Helmut Weber

Hi Shawn,

i'm not too good with this hyperlink stuff.
however, this one seems to work.

Sub FindAndReplaceWithHypertextImproved2()
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
Dim oHpl As hyperlink

sFnd = "August"
sHpl = "http://www.microsoft.com"
sDsp = "Microsoft"

Set rDoc = ActiveDocument.Range
ResetSearch
With rDoc.Find
.Text = sFnd
.Wrap = wdFindStop
Do While .Execute
Set oHpl = ActiveDocument.Hyperlinks.Add _
(rDoc, Address:=sHpl, _
TextToDisplay:=sDsp)
rDoc.End = rDoc.End + Len(sDsp)
rDoc.Collapse wdCollapseEnd
Loop
End With
oHpl.Follow
End Sub

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
G

Greg Maxey

Helmut,

And it works fine :)

Actually you don't need a macro to replace text with a hyperlink. Create
the hyperlink in the document, select it and press ALT+F3 to create a named
Autotext entry. Enter the autotext entry in the document, select it an copy
to clipboard. You can now delete all the above in the document.

Now use Edit>Replace. Type the text you want replaced in the find what
field. Type ^c in the replace with field and click replace all.
 
T

Tony Jollans

You don't actually need to make the autotext entry.

Just type the hyperlink somewhere, select it, copy it, delete it, then do
the find / replace all with ^c
 
H

Helmut Weber

Hi Submariner,
OscarRomeoGolf

that sure will give some people something to think about.
Actually you don't need a macro to replace text with a hyperlink. Create
the hyperlink in the document, select it and press ALT+F3 to create a named
Autotext entry. Enter the autotext entry in the document, select it an copy
to clipboard. You can now delete all the above in the document.

Now use Edit>Replace. Type the text you want replaced in the find what
field. Type ^c in the replace with field and click replace all.

Yes. I'll have to remember that.

That's the drawback with simple solutions.
They aren't that satisfying...

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
G

Greg Maxey

Tony,

Of course you are correct. I was thinking about a macro Graham Mayor and I
worked on a year or so ago to replace with an existing autotext entry. That
coded involved simply replace the text with a copy of the autotext in the
clipboard. That is why I included the extra steps that you correctly
pointed out as unnecessary.
 
S

Shawn G.

Thank you All Very Much, Once again Helmut - You are the Man with the
Answers!!!
I actually need the code as I am using Excel to control several Word Docs,
Thank you again!
 

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