Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I change an existing hyperlink in a Microsoft Word document?

-- PH

SpacerHey, Scripting Guy! AnswerScript Center

Hey, PH. You know, we’re impressed that you had the guts to ask a question today; after all, today is Friday the 13th, a date many people dread, fearing that anything bad that could happen will happen. For example, people who suffer from Paraskevidekatriaphobia (a debilitating fear of Friday the 13th) would never ask the Scripting Guys a question on this day: they’d be too afraid that we’d give them a script that didn’t work, that we’d write a column that didn’t make sense, or that we’d blabber on and on about some arcane trivia that has nothing to do with scripting or with the question they asked.

Superstitious nonsense. Bah. Humbug!

Trivia note. Fridays have long been considered unlucky; according to tradition, Adam and Eve took their fateful bite of the apple on a Friday. (Although we should note that Peter Costantini, the oldest living Scripting Guy, says he remembers the two biting into the apple on a Wednesday morning.) Likewise, the number 13 has long been considered an unlucky number; many ancient cultures believed that it was unlucky to invite 13 people to dinner. (Which would definitely be true if you were the one cooking the dinner.) However, superstitions involving Friday the 13th seem to be a modern phenomenon; no references to Paraskevidekatriaphobia (or anything simlar) can be found prior to the 20th century.

But, like we said, this is all superstitious nonsense. As it turns out, PH, this is your lucky day; after all, we just happened to have a script that can change an existing link in a Microsoft Word document:

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
    If objHyperlink.Address = "http://www.microsoft.com/" Then                                
        objHyperlink.Address = "http://www.microsoft.com/technet/scriptcenter/"
    End If
Next

As you can see, we start out by creating an instance of the Word.Application object; we then set the Visible property to True, giving us a running instance of Microsoft Word that we can see onscreen. (Of course, you don’t have to see Word onscreen in order to change a hyperlink. For educational purposes, however, we’ve always found it better if people can watch as the excitement unfolds.)

With Word up and running we then use the Open method to open the document C:\Scripts\Test.doc. As it turns out, Test.doc happens to have several hyperlinks pointing to http://www.microsoft.com/. That, of course, is the Microsoft.com home page; why the heck would anyone want to go there? Consequently, we need our script to locate all instances of this particular hyperlink and change the target address to the Internet’s true hot spot: http://www.microsoft.com/technet/scriptcenter/.

Note. No, Google has not offered us $1.6 billion for the Script Center, at least not yet. But that’s probably because they know that the Scripting Guys would never sell.

So how do we go about changing these hyperlinks? Well, to begin with, we use this line of code to retrieve a collection of all the hyperlinks in the document:

Set colHyperlinks = objDoc.Hyperlinks

Once we have the collection in hand we set up a For Each loop to loop through each hyperlink in the collection. Inside that loop we use the following line of code to see if the Address property (the property representing the hyperlink target) is equal to http://www.microsoft.com/:

If objHyperlink.Address = "http://www.microsoft.com/" Then

If Address isn’t equal to http://www.microsoft.com/ we skip that hyperlink, loop around, and continue the process with the next item in the collection. If the hyperlink is equal to http://www.microsoft.com/, we then use this line of code to retarget the link to the Script Center instead:

objHyperlink.Address = "http://www.microsoft.com/technet/scriptcenter/"

And then we loop around and check the next link in the collection. When we’re all done all the links that used to point to the Microsoft.com home page will now point to the Script Center. (You can verify that by holding the mouse over each link and viewing the little target address popup that will appear.)

There you have it, PH. Best of all, it turns out that this is an even luckier day for you than we first thought. Not only did we manage to answer your question, but we did it quickly enough that we have time to relate even more historical facts for you. For example, you’re familiar with Noah’s Ark, right? According to legend, the rain began falling on a Friday. The destruction of the Tower of Babel? Occurred on a Friday. And then here’s the --

Note. Sorry, PH; it seems as though Friday the 13th really is an unlucky day. After all, we had several more pages of fascinating Friday facts, but the Scripting Editor seems to have inadvertently deleted them. Please accept our deepest apologies. We’ll see what we can do to retrieve all that information for you. It just wouldn’t be a Hey, Scripting Guy! without it.