Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-03-2023, 01:50 AM
RobiNew RobiNew is offline Add hyperlink to each http(s) string Windows 10 Add hyperlink to each http(s) string Office 2016
Competent Performer
Add hyperlink to each http(s) string
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Question 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
Reply With Quote
  #2  
Old 10-03-2023, 05:58 AM
vivka vivka is offline Add hyperlink to each http(s) string Windows 7 64bit Add hyperlink to each http(s) string Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

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.
Reply With Quote
  #3  
Old 10-03-2023, 08:46 AM
RobiNew RobiNew is offline Add hyperlink to each http(s) string Windows 10 Add hyperlink to each http(s) string Office 2016
Competent Performer
Add hyperlink to each http(s) string
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

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 ':' ?
Reply With Quote
  #4  
Old 10-03-2023, 09:56 AM
vivka vivka is offline Add hyperlink to each http(s) string Windows 7 64bit Add hyperlink to each http(s) string Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

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.
Reply With Quote
  #5  
Old 10-03-2023, 10:13 AM
RobiNew RobiNew is offline Add hyperlink to each http(s) string Windows 10 Add hyperlink to each http(s) string Office 2016
Competent Performer
Add hyperlink to each http(s) string
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Question

Many thanks, Vivka! But Is there a workaround? I need to include also '.' and ':' .
Reply With Quote
  #6  
Old 10-03-2023, 10:39 AM
vivka vivka is offline Add hyperlink to each http(s) string Windows 7 64bit Add hyperlink to each http(s) string Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
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
  #7  
Old 10-03-2023, 02:27 PM
Guessed's Avatar
Guessed Guessed is offline Add hyperlink to each http(s) string Windows 10 Add hyperlink to each http(s) string Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

You could also try
Code:
Sub Macro1()
  Options.AutoFormatAsYouTypeReplaceHyperlinks = True
  ActiveDocument.Range.AutoFormat
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #8  
Old 10-04-2023, 07:14 AM
RobiNew RobiNew is offline Add hyperlink to each http(s) string Windows 10 Add hyperlink to each http(s) string Office 2016
Competent Performer
Add hyperlink to each http(s) string
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Smile

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.
Reply With Quote
  #9  
Old 10-04-2023, 07:33 AM
vivka vivka is offline Add hyperlink to each http(s) string Windows 7 64bit Add hyperlink to each http(s) string Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

RobiNew, you are welcome!
Reply With Quote
Reply



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
Add hyperlink to each http(s) string 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

Other Forums: Access Forums

All times are GMT -7. The time now is 07:22 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