Searching for a url and replace it with some text using Macro

Hi,
I am trying to search for a url and replace it with some text using Macro in word document.
Can some one please help me ?

Thanks,
Mobi.
netianAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
GrahamSkanConnect With a Mentor RetiredCommented:
With  the default Autoformat setting, when you enter URLs into Word, they are converted to Hyperlinks, much as your sample above has.

However there are few absolutely illegal characters in URLs, so it is difficult to make rigorous with Wildcards alone
Sub ReplaceURLs()
    Dim strNewText As String
    Dim strCset As String
    Dim rng As Range
    
    strNewText = "Some text"
    strCset = " " & vbTab & vbCr & vbLf & Chr$(34)
    Set rng = ActiveDocument.Range
    With rng.Find
        .MatchWildcards = True
        .Text = "<[fhpts]{3,5}://"
        Do While .Execute
            rng.MoveEndUntil strCset
            rng.Text = strNewText
            rng.Collapse wdCollapseEnd
            rng.End = ActiveDocument.Range.End
        Loop
    End With
End Sub

Open in new window

0
 
GrahamSkanRetiredCommented:
What problem are you having?

Do Ctrl+H and put the URL in the Find box, and the new text in the Replace box.

Find: http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Word/Q_25498725.html?cid=1763

Replace: Some text

Then click Replace or Replace All


0
 
netianAuthor Commented:
Hi Graham,
I want to do it through macro. It should be an automated task when the word document loads up.

Thanks,
Mubashir.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
GrahamSkanRetiredCommented:
This needs to go in the ThisDocument module of the document's template.
Private Sub Document_Open()
    With ActiveDocument.Range.Find
        .Text = "http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Word/Q_25498725.html?cid=1763"
        .Replacement.Text = "Some text"
        .Execute Replace:=wdReplaceAll
    End With
End Sub

Open in new window

0
 
netianAuthor Commented:
Thanks Graham. But i want a regular expression that can search the text for any URL and then replace it.

Thanks,
Mobi.
0
 
GrahamSkanConnect With a Mentor RetiredCommented:
Are these all Hyperlinks? If so, then this could work.
Private Sub Document_Open()
    Dim rng As Range
    Dim strNewText As String
    Dim hyp As Hyperlink
    Dim bFound As Boolean
    
    strNewText = "Some text"
    bFound = True
    Do
        bFound = False
        For Each hyp In ActiveDocument.Hyperlinks
            If InStr(hyp.Address, "://") Then
                Set rng = hyp.Range
                hyp.Delete
                rng.Text = strNewText
                bFound = True 'Collection disturbed, so restart
                Exit For
            End If
        Next hyp
    Loop While bFound
End Sub

Open in new window

0
 
netianAuthor Commented:
Hi Graham,
They are simple text like www.somedomain.com, and not hyperlinks in word document, i would like to use a regular expression that parses the whole text of the document and then just replaces the url with some other text.

Thanks,
Mobi.
0
 
käµfm³d 👽Connect With a Mentor Commented:
A regex to accomplish this might be as below. This would only work for URLs like what you posted above:  machine.domain.TLD.  If you have sub-directories in the URL, then the pattern would need to be modified.
Dim reg As New RexExp

With reg
    .Pattern = "\w+\.\w+\.(com|net|org|biz)"
    .IgnoreCase = True
    .Global = True
    .Replace YOUR_SEARCH_TEXT_HERE, YOUR_REPLACEMENT_TEXT_HERE
End With

Open in new window

0
All Courses

From novice to tech pro — start learning today.