Consulting

Results 1 to 3 of 3

Thread: Hyperlink selected text in email body

  1. #1

    Hyperlink selected text in email body

    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.

  2. #2
    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
    Last edited by gmayor; 03-22-2019 at 01:52 AM.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    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!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •