![]() |
#1
|
||||
|
||||
![]()
I have trouble finding answer to how to work this perfectly working macro with selection.range.
Code:
Sub AddGPHLink9() Dim oRng As Range Dim strLink As String Set oRng = ActiveDocument.Range With oRng.Find Do While .Execute(findText:="([A-Z]{2}) ([0-9]{4,}) ([A-Z0-9]{1,2})", MatchWildcards:=True) 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 Exit Sub End Sub Tried Replace ActiveDocument.Range with selection.range and oRng.End = oRng.End + 1 But don't have proper knowledge. |
#2
|
||||
|
||||
![]()
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 |
#3
|
||||
|
||||
![]()
Worked like a charm.
|
![]() |
Tags |
word vba |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
paik1002 | Word VBA | 1 | 12-17-2015 04:51 AM |
Selection (range) in Word or Excel table | NobodysPerfect | Word VBA | 2 | 09-16-2014 12:06 AM |
![]() |
Smallweed | Word VBA | 4 | 08-19-2014 07:21 AM |
Selection or Range | Tommes93 | Word VBA | 1 | 04-10-2014 02:50 AM |
Range Name in Adv Filter -> NOt working | 56_kruiser | Excel | 2 | 11-20-2012 09:58 AM |