View Single Post
 
Old 02-18-2016, 01:32 AM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,137
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Replacing ActiveDocument with Selection, will take you part of the way, but you will find that because the scope of oRng changes when using this construction, it will continue searching past the range. You could use a check range to ensure that the replacement is in the original selection e.g. as follows. Note the round brackets around the elements of the search are unnecessary in this instance.

Code:
Sub AddGPHLink9()
Dim oRng As Range
Dim oSelect As Range
Dim strLink As String
    Set oSelect = Selection.Range
    Set oRng = Selection.Range
    With oRng.Find
        Do While .Execute(findText:="[A-Z]{2} [0-9]{4,} [A-Z0-9]{1,2}", MatchWildcards:=True) _
           And oRng.InRange(oSelect)
            strLink = oRng.Text
            strLink = Replace(strLink, Chr(32), "")
            strLink = "website" & strLink & ".com"
            ActiveDocument.Hyperlinks.Add Anchor:=oRng, _
                                          Address:=strLink, _
                                          TextToDisplay:=oRng.Text
            oRng.End = oRng.Fields(1).Result.End
            oRng.Collapse 0
        Loop
    End With
lbl_Exit:
    Set oRng = Nothing
    Set oSelect = Nothing
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote