Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-12-2025, 06:45 AM
RobiNew RobiNew is offline Find and select a passive URL string Windows 11 Find and select a passive URL string Office 2016
Competent Performer
Find and select a passive URL string
 
Join Date: Sep 2023
Posts: 215
RobiNew is on a distinguished road
Default Find and select a passive URL string


Hi, everybody! Can someone help? I'm trying to find and select the whole string of a passive URL. Here below my tentative code, which stops too soon.


Code:
Sub SelectNextURL()
    Dim rng As Range
    Dim startPos As Long
    Dim endPos As Long
    Dim doc As Document
    Dim urlPattern As String
    Dim found As Boolean
    
    Set doc = ActiveDocument
    
    'URLs starting with http or https
    urlPattern = "(https?://[^\s<>""']+)"
    
    ' Use the Find object
    Set rng = doc.Content
    With rng.Find
        .ClearFormatting
        .Text = ""
        .MatchWildcards = True
        ' Word wildcards: http[s]:// followed by any non-space characters
        .Text = "(http*)(://*)([! ^13^32^9^t<>'""]@)"
        found = .Execute
    End With
    
    If found Then
        rng.Select
        MsgBox "URL found and selected: " & rng.Text, vbInformation
    Else
        MsgBox "No URL found.", vbExclamation
    End If
End Sub

Last edited by macropod; 11-12-2025 at 12:51 PM. Reason: Added code tags for code formatting
Reply With Quote
  #2  
Old 11-12-2025, 12:54 PM
macropod's Avatar
macropod macropod is offline Find and select a passive URL string Windows 10 Find and select a passive URL string Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,503
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

See: https://www.msofficeforums.com/52953-post2.html
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old Yesterday, 12:40 AM
RobiNew RobiNew is offline Find and select a passive URL string Windows 11 Find and select a passive URL string Office 2016
Competent Performer
Find and select a passive URL string
 
Join Date: Sep 2023
Posts: 215
RobiNew is on a distinguished road
Default

Thanks a lot, macropod! But the code here below cannot find anything. My fault?
Code:
Sub MakeLinksMP()
Application.ScreenUpdating = False
With ActiveDocument
  With .Range
    With .Find
      .Text = "https://[! ]{1,}"
      .Execute
    End With
If Not .Find.found Then MsgBox "Not found"
    Do While .Find.found
      .Duplicate.AutoFormat
      .Collapse wdCollapseEnd
      .Find.Execute
    Loop
  End With
End With
Application.ScreenUpdating = True
End Sub

Last edited by macropod; Yesterday at 05:24 PM. Reason: Added code tags - again
Reply With Quote
  #4  
Old Yesterday, 05:14 AM
gmaxey gmaxey is offline Find and select a passive URL string Windows 10 Find and select a passive URL string Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,626
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

You need .MatchWildCards = true


Code:
Sub MakeLinks()
'Adapted from code posted by Paul Edstein
  Application.ScreenUpdating = False
  With ActiveDocument
    With .Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "<[0-9A-ÿ.\-]{1,}\@[0-9A-ÿ\-.]{1,}"
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchWildcards = True
      End With
      Do While .Find.Execute
        .Duplicate.AutoFormat
        .Collapse wdCollapseEnd
      Loop
    End With
    With .Range
      With .Find
        .Text = "http://[! ]{1,}"
        .MatchWildcards = True
      End With
      Do While .Find.Execute
        'If you want to remove the "http://" part, unstet next line.
        '.Text = Mid(.Text, 8, Len(.Text) - 7)
        .Duplicate.AutoFormat
        .Collapse wdCollapseEnd
      Loop
    End With
  End With
  Application.ScreenUpdating = True
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #5  
Old Yesterday, 08:37 AM
RobiNew RobiNew is offline Find and select a passive URL string Windows 11 Find and select a passive URL string Office 2016
Competent Performer
Find and select a passive URL string
 
Join Date: Sep 2023
Posts: 215
RobiNew is on a distinguished road
Default

Thanks, gmaxey! But the macro returns "Do While .Find.Execute" Not valid!!.
Reply With Quote
  #6  
Old Yesterday, 03:47 PM
gmaxey gmaxey is offline Find and select a passive URL string Windows 10 Find and select a passive URL string Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,626
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

Did you modify that code in some way? It runs here without error.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #7  
Old Yesterday, 05:26 PM
macropod's Avatar
macropod macropod is offline Find and select a passive URL string Windows 10 Find and select a passive URL string Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,503
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by RobiNew View Post
Thanks a lot, macropod! But the code here below cannot find anything. My fault?
The code in the link works just fine. You could even have just copied & pasted it as is into your own document.

As Greg said, you at least need .MatchWildCards = true

PS: When posting code, please use the code tags - inserted by the # button on the posting menu
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #8  
Old Today, 12:32 AM
RobiNew RobiNew is offline Find and select a passive URL string Windows 11 Find and select a passive URL string Office 2016
Competent Performer
Find and select a passive URL string
 
Join Date: Sep 2023
Posts: 215
RobiNew is on a distinguished road
Default

Yes, I just copied & pasted it as is into Normal.dotm. But the result is always the same: "Do While .Find.Execute" Not valid!!


I am sorry about the missing code tags! I always use the # button on the posting menu, but it doesn't work. And I can prove it.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Find string, Replace with dot tab dhapp Word 5 03-27-2023 07:23 AM
Find and select all string of simlar pattern anon123 Word 4 04-20-2016 11:41 PM
How to find all string within string. PRA007 Word VBA 18 02-12-2016 08:11 PM
Find and select a passive URL string Why is this Find string not working TechEd Word VBA 5 07-05-2014 08:12 PM
Find and select a passive URL string Bad view when using Find and Find & Replace - Word places found string on top line paulkaye Word 4 12-06-2011 11:05 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:56 AM.


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