• Word Macro to extract URLs from links

    Author
    Topic
    #503894

    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 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.
    ~
    Alienware Aurora R6; Win10 Home x64 1803; Office 365 x32
    i7-7700; GeForce GTX 1060; 16GB DDR4 2400; 1TB SSD, 256GB SSD, 4TB HD

    Viewing 0 reply threads
    Author
    Replies
    • #1544881

      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
      [Fmr MS MVP - Word]

      • #1544971

        Try:

        Works like a charm! Thank you very much, macropod.

        Lugh.
        ~
        Alienware Aurora R6; Win10 Home x64 1803; Office 365 x32
        i7-7700; GeForce GTX 1060; 16GB DDR4 2400; 1TB SSD, 256GB SSD, 4TB HD

    Viewing 0 reply threads
    Reply To: Word Macro to extract URLs from links

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: