Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-17-2016, 11:25 PM
PRA007's Avatar
PRA007 PRA007 is offline Working with Selection.range. Windows 7 64bit Working with Selection.range. Office 2010 32bit
Competent Performer
Working with Selection.range.
 
Join Date: Dec 2014
Location: Ahmedabad, Gujrat, India
Posts: 145
PRA007 is on a distinguished road
Default Working with Selection.range.


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
I want to replace ActiveDocument.Range with selection.range
Tried Replace ActiveDocument.Range with selection.range and oRng.End = oRng.End + 1 But don't have proper knowledge.
Reply With Quote
  #2  
Old 02-18-2016, 01:32 AM
gmayor's Avatar
gmayor gmayor is offline Working with Selection.range. Windows 10 Working with Selection.range. Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,106
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 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
  #3  
Old 02-19-2016, 12:52 AM
PRA007's Avatar
PRA007 PRA007 is offline Working with Selection.range. Windows 7 64bit Working with Selection.range. Office 2010 32bit
Competent Performer
Working with Selection.range.
 
Join Date: Dec 2014
Location: Ahmedabad, Gujrat, India
Posts: 145
PRA007 is on a distinguished road
Default

Worked like a charm.
Reply With Quote
Reply

Tags
word vba



Similar Threads
Thread Thread Starter Forum Replies Last Post
Working with Selection.range. Search and reduce the range of a text selection 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
Working with Selection.range. Selection.EndKey not working in 2010 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

Other Forums: Access Forums

All times are GMT -7. The time now is 11:16 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft