Home > How to, Writing code > How to extract urls from MS Word 2007 hyperlinks

How to extract urls from MS Word 2007 hyperlinks

by Kathy Purdy on December 7, 2008

Recently I have been creating a series of “cheat sheets” on various topics related to blogging and social media using Microsoft Word 2007. These documents are essentially an annotated list of websites related to a particular topic, which I intend to provide to my clients as an aid to their own education and research.

Having long been a WordPerfect user, I was pretty happy with how these documents were looking and how easy it was to create the hyperlinks for them. But suddenly I realized that if I wanted to print one out and hand it to someone, they would have no way of knowing where the hyperlink was pointing to. The url for each link wasn’t visible.

Surely, I thought, I was not the first person to run into this problem. Surely there is an option on some menu that will enable me to display the urls when printing. But I searched the help file to no avail. I checked out three different books on the new version of Word out of the library. Nothing.

You might think it would be easier to copy and paste each url into the document in the appropriate place than to spend time researching a software based solution. But I had already created nine of these documents, and many had over two dozen links in them. That was too much copying-and-pasting for someone who has repetitive strain issues. It was worth it to me to keep hunting for a solution that would not bring on a bout of “tennis” elbow.

I finally got help at a Google Group. Yves, from Belgium, provided me with a macro that gets the job done:

Public Function ConvertHyperlinks()
    Dim fld As Field

    For Each fld In ActiveDocument.Fields
        If fld.Type = wdFieldHyperlink Then
            fld.Select
            Selection.InsertAfter (" (" + Replace(Split(fld.Code.Text, " ")(1), Chr(34), "") + ")")
        End If
    Next

End Function

No, I don’t know what it all means. I had a family member who is familiar with Visual Basic for Applications (VBA) look it over to make sure it wasn’t going to do something harmful before I tried it on one of my documents. To do that, I had to teach myself how to use the macro editor, but the library books helped with that.

This is just the latest example of the help I have gotten from total strangers who shared freely of their knowledge and time. I hope someone else faced with the same problem finds this helpful.

{ 11 comments… read them below or add one }

Conrad September 29, 2009 at 6:34 pm

that is really amazing! I’ve been looking for something writing the url just behind the text, but here it just sais ” (HYPERLINK)”
did you chane anything on the macro script?

Kathy Purdy September 29, 2009 at 7:51 pm

I didn’t change anything in the macro script, Conrad. I’m sorry; I don’t understand what your problem is.

Conrad September 30, 2009 at 6:47 pm

I see.. well the problem is following:
I have a text and a link like an <a> tag in html within my word document. But when applying this macro (using the “create a new macro” command on developer-tab, inserting the script above and pressing play) it just writes the word “(HYPERLINK)” behind everyone of my linked-text. I was expecting the url of the text within the brackets after every link.

Kathy Purdy September 30, 2009 at 7:24 pm

I think it is because you and I were starting from different places. In my original document, the text was already a hyperlink. In other words, you couldn’t see the anchor tag, the text was “clickable.” And what the macro does is make it so you can see the url, because when it’s printed on paper you can’t click on the links. If you want help modifying this macro you should contact Yves, because I don’t know how to change it.

Conrad October 2, 2009 at 2:43 am

It’s the same in my text.. blue underlined clickable links.. weird.. I will contact Yves and let you know if I have the solution :)
thanks for the advice

Arnout Lansberg January 8, 2010 at 8:49 am

Hello Conrad, I have exactly the same problem using Kathy’s macro ( ‘HYPERLINK’). Did you happen to find the cause/solution yet?

Thanks!

Conrad January 8, 2010 at 6:36 pm

Unfortunately the makros did not work as expected. Before I was going to contact Yves, I tried the following and it worked out well:
I saved the word document as a .html file and opened it with firefox. With the plugin “webdeveloper’s toolbar” you can easily list out every link in a website (click: Information -> View Link Information) and then copy the urls.

I hope this helps a bit.

Knut January 11, 2010 at 4:41 am

I have a similar problem, having to extract close to 400 hyperlinks from a Word document.

I found a script on Microsoft’s Technet pages which promises to do exactly that:
http://technet.microsoft.com/en-us/library/ee692879.aspx

I have not yet tested it, but crossing my fingers that this what is needed…

The script is as follows:

Set objWord = CreateObject(“Word.Application”)
objWord.Visible = True

Set objDoc = objWord.Documents.Open(“c:\scripts\test.doc”)

Set colHyperlinks = objDoc.Hyperlinks

For Each objHyperlink in colHyperlinks
Wscript.Echo objHyperlink.Address
Wscript.Echo objHyperlink.TextToDisplay
Next

Variants are also given in the article for exporting the hyperlinks to ‘My favourites’ or to an HTML document.

Enrico Poli February 6, 2011 at 2:14 pm

I had the same problem as Conrad.

I merged the script from Yves with one found on http://bit.ly/151sJ8, and the result seems to work for me (see below). Caveat: this was just an hack, I’m not a VBA programmer, and I struggle to remember how to open the VB editor from MS Word. All I can say is: this seems to work for me.

Sub HlinkChanger()
Dim oRange As Word.Range
Dim oField As Field
Dim link As Variant
With ActiveDocument
.Range.AutoFormat
For Each oRange In .StoryRanges
For Each oFld In oRange.Fields
If oFld.Type = wdFieldHyperlink Then
For Each link In oFld.Result.Hyperlinks
oFld.Select
Selection.InsertAfter (” (” + link.Address + “)”)
Next link
End If
Next oFld
Set oRange = oRange.NextStoryRange
Next oRange
End With
End Sub

Enrico Poli February 6, 2011 at 2:33 pm

Even better solution (see here for another piece of my frankenstein code: http://bit.ly/gydBq0), this piece of VBA will insert the links *as footnotes*. As I don’t understand fully what I’m doing, I guess it could be optimized, but it does work for me, even on longish documents, and I love it :-)

Sub HlinkChanger()
Dim oRange As Word.Range
Dim oField As Field
Dim link As Variant
With ActiveDocument
.Range.AutoFormat
For Each oRange In .StoryRanges
For Each oFld In oRange.Fields
If oFld.Type = wdFieldHyperlink Then
For Each link In oFld.Result.Hyperlinks
oFld.Select
Selection.MoveRight Unit:=wdCharacter, Count:=1
With Selection
With .FootnoteOptions
.Location = wdBottomOfPage
.NumberingRule = wdRestartContinuous
.StartingNumber = 1
.NumberStyle = wdNoteNumberStyleArabic
End With
.Footnotes.Add Range:=Selection.Range, Reference:=”"
End With
Selection.TypeText Text:=link.Address
ActiveWindow.View.SeekView = wdSeekMainDocument
Next link
End If
Next oFld
Set oRange = oRange.NextStoryRange
Next oRange
End With
End Sub

Vincent November 12, 2011 at 12:11 pm

WOW!…You guys are just GREAT.. I was struggling for HOURS to try and extrapolate 1000′s of links from my documents and then found this site and the solutions all worked fantastic. Thanks guys!

Leave a Comment

Previous post:

Next post: