Results 1 to 3 of 3
  1. #1
    5 Star Lounger Lugh's Avatar
    Join Date
    Jun 2010
    Location
    Indy
    Posts
    626
    Thanks
    168
    Thanked 77 Times in 68 Posts

    Word Macro to extract URLs from links

    For Word 2010 and/or 2013

    I have a few Word documents with links, one per line/paragraph, and I need to extract the underlying URL and paste it after the link text on the same line/para. Example:

    Windows Secrets
    Microsoft
    Google

    etc [I don't seem to be able to make links in this post, only URLs] Above would be live clickable links in my docs.

    Desired result:
    Windows Secrets: http://windowssecrets.com/
    Microsoft: http://microsoft.com/
    Google: http://google.com/

    So the link becomes unclickable text, followed by the clickable URL in full.

    I've looked at the Editing section of the Home tab, and Hyperlink in the Links section of the Insert tab, but the best macro I could record [using click on Hyperlink to bring up the dialog box with the URL in it] copied the URL literally--so I ended up pasting the same URL on each line, ie
    Windows Secrets: http://windowssecrets.com/
    Microsoft: http://windowssecrets.com/
    Google: http://windowssecrets.com/

    I can record and edit macros, but not write them from scratch. Thanks for any help or pointers.
    Lugh.
    ~
    Windows 10 Pro x64 1607; Office 2016 (365 Home) x32; Win Defender, MBAM Pro

    ASRock H97 Anniversary; Xeon E3-1231V3 (like i7)
    Gigabyte GeForce GTX 970; 12GB Crucial DDR3 1600
    Logitech MX Master mouse; Roccat Isku kb

  2. #2
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Try:
    Code:
    Sub Demo()
    Application.ScreenUpdating = False
    Dim i As Long, Rng As Range
    With ActiveDocument
      For i = .Hyperlinks.Count To 1 Step -1
        With .Hyperlinks(i)
          Set Rng = .Range
          Rng.InsertAfter ": "
          Rng.Collapse wdCollapseEnd
          Rng.FormattedText = .Range.FormattedText
          Rng.Hyperlinks(1).TextToDisplay = .Address
          .Range.Fields(1).Unlink
        End With
      Next
    End With
    Application.ScreenUpdating = True
    End Sub
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  3. The Following User Says Thank You to macropod For This Useful Post:

    Lugh (2016-01-04)

  4. #3
    5 Star Lounger Lugh's Avatar
    Join Date
    Jun 2010
    Location
    Indy
    Posts
    626
    Thanks
    168
    Thanked 77 Times in 68 Posts
    Quote Originally Posted by macropod View Post
    Try:
    Works like a charm! Thank you very much, macropod.
    Lugh.
    ~
    Windows 10 Pro x64 1607; Office 2016 (365 Home) x32; Win Defender, MBAM Pro

    ASRock H97 Anniversary; Xeon E3-1231V3 (like i7)
    Gigabyte GeForce GTX 970; 12GB Crucial DDR3 1600
    Logitech MX Master mouse; Roccat Isku kb

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •