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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

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
GrahamSkanRetiredCommented:
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
GrahamSkanRetiredCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
käµfm³d 👽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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.