How Can I Add a Hyperlink to a Word Document?

How Can I Add a Hyperlink to a Word Document?

  • Comments 1
  • Likes
Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I add a hyperlink to a Word document?

-- BA

SpacerHey, Scripting Guy! AnswerScript Center

Hey, BA. Interesting question; in thinking about common tasks for scripting Microsoft Office applications we never even considered adding a hyperlink to a document. (Needless to say, that’s why we rely on people like you to let us know about the tasks people really want to script.) As it turns out, it’s pretty easy to add a hyperlink to a Microsoft Word document; in fact, we were able to do it with just a few lines of code:

Set objWord = CreateObject("Word.Application")
objWord.Visible = TRUE
Set objDoc = objWord.Documents.Add()
Set objRange = objDoc.Range()
Set objLink = objDoc.Hyperlinks.Add _
    (objRange, " http://www.microsoft.com/technet/scriptcenter ", , , "Script Center")

The first three lines in this script are just boilerplate code. We start out by creating an instance of the Word.Application object. We then set the Visible property to TRUE because, as a demonstration script, we want to make sure you can see what’s happening to the document. Next we create a new document, which gives us a blank piece of paper to work on. In line 4, we create an instance of the Range object; we’ll use this to tell Word that we want to create the hyperlink at the current cursor location. If you’ve done any scripting in Word, this should all look very familiar.

That brings us to this line of code, which actually creates the hyperlink:

Set objLink = objDoc.Hyperlinks.Add _
    (objRange, " http://www.microsoft.com/technet/scriptcenter ", , , "Script Center")

What we do here is call the Add method to add a new link to the Hyperlinks collection. When we do this, we need to pass the Add method a few parameters:

Parameter

Description

objRange

Hyperlink “anchor,” which indicates the spot in the document where we want to create the hyperlink. In this sample script, we’re creating the hyperlink right at the start of the document.

http://www.microsoft.com/technet/scriptcenter

URL we want to link to.

blank

Enables us to link to a bookmark or other specified section within the HTML document. We aren’t linking to a bookmark in this script, so we leave this parameter blank.

blank

Optional tooltip that appears when you hold the mouse over the hyperlink. We aren’t using a tooltip in this sample script, so we leave this parameter blank.

Script Center

Text for the hyperlink (that is, the text you see in the document, which may or may not be the destination URL).

blank

Enables us to specify the window or frame in which the HTML document opens. We don’t specify a target in this sample script, but if we wanted to open the Web page in a new window we could specify “_blank” as the target.

When we finish running the script we should have a new hyperlink that looks - and functions - just like this one:

Script Center

That should do the trick. Give it a try and see what happens.

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment