PDA

View Full Version : Hyperlink selected text in email body



perkocet21
03-21-2019, 01:57 PM
Email text:
Text text blah blah contractor blah blah text.

All I want to do is highlight a word (contractor, in this case), then have a macro add a hyperlink to (http-examplewebpage) with contractor (the selection) displayed as the text. I need to have a few different variations of this with different words, so it can't be specific to hyperlinking contractor, it just needs to be a hyperlink to the selected text. It seems simple, and I've found nothing that does this as easily as I think it should be able to be done. I tried recording the macro in Word but can't get it to translate and work in Outlook.

Can somebody do in 30 seconds what I've been trying to do for the last two hours? Thanks.

gmayor
03-21-2019, 10:20 PM
Why do you need a macro for this? Select the word and click CTRL+K. The word will be the text of the hyperlink. You just need to add the address.

If you need a macro then you need something like the following which will replace the selected text with the named link.


Sub AddLink()
Dim oRng As Object
Dim oLink As Object
Dim strLinkText As String
Const strLink As String = "https://www.fedex.com/apps/fedextrack/" 'the link
On Error GoTo ErrHandler
If TypeName(ActiveWindow) = "Inspector" Then
If ActiveInspector.IsWordMail And ActiveInspector.EditorType = olEditorWord Then
Set oRng = ActiveInspector.WordEditor.Application.Selection.Range
strLinkText = Trim(oRng.Text)
Set oLink = oRng.hyperlinks.Add(Anchor:=oRng, _
Address:=strLink & strLinkText, _
SubAddress:="", _
ScreenTip:="", _
TextToDisplay:=strLinkText)
Set oRng = oLink.Range
oRng.collapse 0
oRng.Select
End If
End If
lbl_Exit:
Set oRng = Nothing
Set oLink = Nothing
Exit Sub
ErrHandler:
Beep
Err.Clear
GoTo lbl_Exit

perkocet21
03-22-2019, 05:24 AM
That'll do. I want to use this repetitively with a couple different links, so I'll replicate the macro, adjust the link, and add to my quick access toolbar. Thanks for the help!