PDA

View Full Version : Adding a hyperlink while replacing



tracer
03-20-2012, 12:51 AM
Hi,

I've been trying to wrap my head arround a problem I've got but without success :)

I need to search for a code in a word document and replace all instances of this code (e.g. ABC ) into a word ( e.g. AaBbCc )

I' got no problem replacing the code with the word but I have no clue how to add a hyperlink to this word.

Any hints would be welcome.

Below is the code I use to replace.



With ActiveDocument.Content.Find
.ClearFormatting
.MatchCase = True
.Format = False
.Text = "ABC"

With .Replacement
.ClearFormatting
.Text = "AaBbCc"
End With
.Execute Forward:=True, Replace:=wdReplaceAll
End With

macropod
03-20-2012, 01:27 AM
The simplest way is to create your hyperlink, copy it to the clipboard, then use a standard Find/Replace, where:
Find = ABC
Replace = ^c
No code required.

tracer
03-20-2012, 01:48 AM
Thanks for your reply.
Problem is that the word that needs to replace the code is not the hyperlink itself and I would eventually like to use a list ( from excel ) that searches predefined code's linked to words linked to hyperlinks..

The searching in excel is a different part of code that works fine.. I just need to find a way to dynamicly create a hyperlink and "attach" it to the word that i'm replacing the code with :)

If that makes any sense

e.g. ABC is replaced by AaBbCc which links to www . vbaexpress . com/AaBbCc

macropod
03-20-2012, 03:03 AM
It would have helped if you had said Excel is involved, and that there are many different strings to replace.

Given your additional parameters, see:
http://www.msofficeforums.com/vba/11654-interesting-hyperlink-macro-problem.html
With the code in that link, it's a simple matter to add a third column to the Excel data for the hyperlink display text, and the appropriate variables, etc to output that hyperlink display text in the Word file.

tracer
03-20-2012, 03:06 AM
My apologies, I wanted to build up my macro and just wanted to see if I could get it to replace with a link first.

Thanks for the link, I'll take a look at that and see if that'll do the trick for me

macropod
03-20-2012, 04:11 AM
A Find/Replace operation cannot create hyperlinks - you need to provide them directly, via the clipboard, and, to do that, you would need them to be created in Word beforehand. Consequently, a different approach is needed; viz. the Hyperlinks.Add method.

akuini
03-20-2012, 06:16 AM
Hi, tracer
Maybe this is what you want

Sub tryHyperlink()
Dim Rng As Range
Set Rng = ActiveDocument.Range

With Rng.Find
.ClearFormatting
.MatchCase = True
.Format = False

Do While .Execute(FindText:="ABC", ReplaceWith:="AaBbCc", Forward:=True) = True

ActiveDocument.Hyperlinks.Add Anchor:=Rng, _
Address:="www.vbaexpress.com/" & Rng
Rng.Collapse wdCollapseEnd

Loop
End With
End Sub

macropod
03-20-2012, 02:51 PM
Hi akuini,

As tracer said, the hyperlinks and the display text to go with them are in an external workbook. Your macro method does nothing more than the ordinary non-macro Find/Replace I posted initially.

akuini
03-21-2012, 08:24 PM
Hi, macropod
You're right, my macro does nothing more than the ordinary non-macro Find/Replace you posted initially. But I think tracer needs some code just for starter to build up new macro to accomplished the complete task. I only responded to this:


The searching in excel is a different part of code that works fine.. I just need to find a way to dynamicly create a hyperlink and "attach" it to the word that i'm replacing the code with :)

If that makes any sense

e.g. ABC is replaced by AaBbCc which links to www . vbaexpress . com/AaBbCc

So then he can modify the macro in order to relate to a list he searched ( from excel ).
Sorry, I didn't make myself clear that I only responded to that. And maybe I'm wrong about what tracer wanted. Sorry again. (Actually, I can't speak english well, it's not my native language. I used Google Translate just to write this post).

And actually I just learnt from your post about how to do it by using ordinary non-macro Find/Replace. So, thanks.