none
List Hyperlinks in Document RRS feed

Answers

  • Hi Chris,

    For the hyperlink addresses, try:

    Sub ListLinks()
    Dim hLnk As Hyperlink
    With ActiveDocument.Range
      For Each hLnk In .Hyperlinks
        .InsertAfter vbCrLf & hLnk.Address
      Next
    End With
    End Sub

    An alternative approach would be to select the hyperlinks as the macro goes, so you can see what it's finding:

    Sub ListLinks()
    Dim hLnk As Hyperlink
    With ActiveDocument.Range
      For Each hLnk In .Hyperlinks
        .InsertAfter vbCrLf & hLnk.Address
        hLnk.Range.Select
        MsgBox hLnk.Address
      Next
    End With
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]


    • Edited by macropodMVP Monday, April 8, 2013 12:10 AM Code fix
    • Marked as answer by KCDW Tuesday, April 9, 2013 2:23 PM
    Sunday, April 7, 2013 8:58 AM
  • For internal document links, after:
    .InsertAfter vbCrLf & hLnk.Address
    insert:
    If hLnk.SubAddress <> "" Then .InsertAfter ":" & hLnk.SubAddress

    Both versions of the code work for me.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by KCDW Tuesday, April 9, 2013 2:23 PM
    Monday, April 8, 2013 9:57 PM

All replies

  • Chris : Does this helps as start ?

    Namespace forumCode
    	Class Program
    		Private Shared Sub Main(args As String())
    			'Add reference to Microsoft.Office.Interop.Word 
    			Dim application As New Application()
    			Dim document As Document = application.Documents.Open("C:\temp\TEST.docx")
    			Dim objLinks As Hyperlinks = document.Hyperlinks
    			For i As Integer = 1 To objLinks.Count
    				Dim link As Hyperlink = objLinks.get_Item(i)
    				Console.WriteLine(link.Name)
    			Next
    			application.Quit()
    		End Sub
    	End Class
    End Namespace

    Friday, April 5, 2013 5:21 PM
  • Hi Chris,

    The following macro outputs all the hyperlinks at the end of the active document.

    Sub ListLinks()
    Dim i As Long
    With ActiveDocument
      For i = 1 To .Hyperlinks.Count
        Hyperlinks(i).Range.Copy
        With .Range
          .InsertAfter vbCrLf
          .Characters.Last.Paste
        End With
      Next
    End With
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Friday, April 5, 2013 11:01 PM
  • Chris : Does this helps as start ?

    Sorry, I don't know what to do with this code

    Thank you!

    Chris Ward

    Sunday, April 7, 2013 8:03 AM
  • Hi Chris,

    The following macro outputs all the hyperlinks at the end of the active document.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Thanks Paul,

    This code recreates or copies hyperlinks to the end of a file except hyperlinks within pictures. It errors on Pictures. If possible I would like for the display text not be what is displayed but rather the actual Hyperlinks whether they are textual or pictures or whatever else they may be hidden in. I have a document being distributed that I found has hidden hyperlinks. Since there are hundreds of pages I would like to find them all to investigate what they are about.

    Thank you!!!


    Chris Ward

    Sunday, April 7, 2013 8:10 AM
  • Hi Chris,

    For the hyperlink addresses, try:

    Sub ListLinks()
    Dim hLnk As Hyperlink
    With ActiveDocument.Range
      For Each hLnk In .Hyperlinks
        .InsertAfter vbCrLf & hLnk.Address
      Next
    End With
    End Sub

    An alternative approach would be to select the hyperlinks as the macro goes, so you can see what it's finding:

    Sub ListLinks()
    Dim hLnk As Hyperlink
    With ActiveDocument.Range
      For Each hLnk In .Hyperlinks
        .InsertAfter vbCrLf & hLnk.Address
        hLnk.Range.Select
        MsgBox hLnk.Address
      Next
    End With
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]


    • Edited by macropodMVP Monday, April 8, 2013 12:10 AM Code fix
    • Marked as answer by KCDW Tuesday, April 9, 2013 2:23 PM
    Sunday, April 7, 2013 8:58 AM
  • All the codes return the same error

    Object doesn't support this property or method.

    Do I need to select any other references?


    Chris Ward

    Sunday, April 7, 2013 7:43 PM
  • Code fixed (omitted .Range).

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Monday, April 8, 2013 12:11 AM
  • Hi macropod,

    The 1st Code seems to work on Internet hyperlinks but not hyperlinks within the document.

    The second code still does not work.


    Chris Ward

    Monday, April 8, 2013 8:58 PM
  • Chris : Does this helps as start ?

    Namespace forumCode
    	Class Program
    		Private Shared Sub Main(args As String())
    			'Add reference to Microsoft.Office.Interop.Word 
    			Dim application As New Application()
    			Dim document As Document = application.Documents.Open("C:\temp\TEST.docx")
    			Dim objLinks As Hyperlinks = document.Hyperlinks
    			For i As Integer = 1 To objLinks.Count
    				Dim link As Hyperlink = objLinks.get_Item(i)
    				Console.WriteLine(link.Name)
    			Next
    			application.Quit()
    		End Sub
    	End Class
    End Namespace

    Ki VenKat,

    Using Office 2007. I do not have that reference available. Will not compile.


    Chris Ward

    Monday, April 8, 2013 9:00 PM
  • For internal document links, after:
    .InsertAfter vbCrLf & hLnk.Address
    insert:
    If hLnk.SubAddress <> "" Then .InsertAfter ":" & hLnk.SubAddress

    Both versions of the code work for me.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by KCDW Tuesday, April 9, 2013 2:23 PM
    Monday, April 8, 2013 9:57 PM
  • Thanks macropod,

    The first code works perfect at making a list.

    The second code does not run as given. It compiles but then it gives an error message. If I comment out the line 'hLnk.Range.Select then there is a msgbox popup that will list 1 hyperlink at a time, once you click ok it moves to the next. Otherwise it seems to work but I just had to click 125 times to get it to finish. Both ok and X pops up the next one.


    Chris Ward

    Tuesday, April 9, 2013 2:28 PM