View Single Post
 
Old 10-03-2023, 10:39 AM
vivka vivka is offline Windows 7 64bit Office 2016
Expert
 
Join Date: Jul 2023
Posts: 293
vivka is on a distinguished road
Default

I think everything will depend on the number of '.' and ':' in each specific url. In https://www.msofficeforums.com/word-vba there's one colon and two periods/full stops, so the colon to find will be the second one and the period to find will be the 3rd one. Other urls may have different numbers of '.' and ':', which means that the code should take into account all posiible options. But that's impossible. At least I dont' know how to solve this. Besides, as far as I know urls may also include ',' and ';', so they also should be included in the code to be on the safe side. As a way out, it is possible to move rng's end (Cset) until space, and if it is preceded by '.,:;' move rng's end one char to the left, then add a hyperlink. Something like this:
Sub Hlink_https()
'Add hyperlinks to all http(s) in the active doc.

Dim Rng As range
Dim SearchString As String
Dim Link As String
Set Rng = ActiveDocument.range
SearchString = "http"
With Rng.Find
.MatchWildcards = True
Do While .Execute(FindText:=SearchString, Forward:=False) = True
Rng.MoveEndUntil Cset:=" "
If Rng.Characters.Last Like "[,.:;]" Then
Rng.MoveEnd unit:=wdCharacter, count:=-1
End If
Link = Rng.text
ActiveDocument.Hyperlinks.Add Anchor:=Rng, _
Address:=Link, _
SubAddress:="", ScreenTip:="", TextToDisplay:=Rng.text
Rng.Collapse wdCollapseStart
Loop
End With
End Sub

Last edited by vivka; 10-03-2023 at 12:44 PM.
Reply With Quote