#1
|
|||
|
|||
Add hyperlink to each http(s) string
The macro here below works perfectly when the string is followed by a space. How do I modify the code so as to make it work also when the string is followed by , ; . : etc.? Can someone help?
Code:
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.MoveStartUntil ("http") Rng.MoveEndUntil Cset:=" " Link = Rng.Text ActiveDocument.Hyperlinks.Add Anchor:=Rng, _ Address:=Link, _ SubAddress:="", ScreenTip:="", TextToDisplay:=Rng.Text Rng.Font.Color = 15597568 'codice di Word Rng.Collapse wdCollapseStart Loop End With |
#2
|
|||
|
|||
Hi, RobiNew! Try replacing your Cset with:
Cset:="[ .,:;]" Besides, although I didn't check it, I don't think the following line is needed, because we work with range: Rng.MoveStartUntil ("http") Another point to consider: if a hyperlink is followed by any number of chars other than space, these chars will be included into the hyperlink, which may result in wrong addressing, so I think adding puctuation marks to Cset is not needed. |
#3
|
|||
|
|||
Thanks! You're quite right about the superfluos 'Link'-line in the code. But space or puctuation marks in Cset are needed, because the string with http is not an active hyperlink. Unfortunately, Cset:="[ .,:;]" (which I had tried myself before posting) does not work: the line Rng.MoveEndUntil Cset:="[ .,:;]" causes no error but it doesn't cover the whole string. It works only if I use Cset:="[ ,;]". But what about '.' and ':' ?
|
#4
|
|||
|
|||
RobiNew, obviously, mistakes are due to the presence of full stops and semicolons in urls (e.g.: https://www.msofficeforums.com/word-vba) and the macro finds the first instances of '.' and ':' which go before all other chars contained in Cset. I think, the most reliable separator is space, which is used by default because urls don't allow spaces.
|
#5
|
|||
|
|||
Many thanks, Vivka! But Is there a workaround? I need to include also '.' and ':' .
|
#6
|
|||
|
|||
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. |
#7
|
||||
|
||||
You could also try
Code:
Sub Macro1() Options.AutoFormatAsYouTypeReplaceHyperlinks = True ActiveDocument.Range.AutoFormat End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#8
|
|||
|
|||
Thank you very much indeed, Vivka! That is exactly what I needed and it works perfectly. Many thanks also to Guessed: I'll keep your code for a different need.
|
#9
|
|||
|
|||
RobiNew, you are welcome!
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
http/1.0 404 not found | Powerpoint User | PowerPoint | 2 | 11-20-2016 11:10 PM |
Find whether string contains hyperlink and Process the same | PRA007 | Word VBA | 15 | 12-01-2015 02:57 AM |
Multiple Hyperlink to a single string | PRA007 | Word VBA | 7 | 11-09-2015 04:29 PM |
Outlook http server | joelwelford | Outlook | 0 | 09-07-2015 10:00 AM |
Way to search for a string in text file, pull out everything until another string? | omahadivision | Excel Programming | 12 | 11-23-2013 12:10 PM |