Adding Hyperlinks to Locked Forms in MS Word
~ by Dian D. Chapman, Technical Consultant
In this potentially paperless world, more and
more folks are learning to take advantage of a wonderful feature in Word
that allows them to create online forms. Microsoft Word’s form feature is an
easy way to create a form that users can fill out on their computers by
simply tabbing from field to field, entering the necessary information as
they move through the document. And by learning some basic VBA (Word’s
programming language) you can enhance these online forms to create slick
AutoForms that can just about run themselves, with only a little user
intervention.
To create an online form,
you just add your boilerplate text (the document text for the form),
add a few form fields into a document, and then click Tools/Protect
Document/Forms to lock the boilerplate text away from modification,
allowing the fields to become activated. The user can then tab from
field to field without the ability to modify any of the boilerplate text in
the document itself.
The Problem
However, because the document text is locked, the user is unable to
interact with any items in the document, other than the areas available
within the form fields. So what if you wanted to add a hyperlink in your
document, possibly to your company web site so the user can easily navigate
to more information? If you try to put a standard hyperlink in the form (by
hitting Ctrl/K), it won’t work because the text is locked.
You can get around this
problem by using a little field nesting trick with a little VBA
programming code to your form. Give it a try with me and you’ll see how easy
it can be.
Open a blank page in Word.
Hit Ctrl/K and enter a hyperlink into the page. Hit Enter a
couple of times to add a little space. Now click View/Toolbars and
toggle on the Forms toolbar. Add a Text form field to your
page. Hit Enter again and add another Text form field to the
page. We’ll pretend this is a sophisticated form and you want your users to
be able to use the hyperlink. To finalize the form and activate the fields,
click Tools/Protect Document and select the Forms option. Don’t
bother adding a password for this test.
Now test your form. You’ll
see that you can easily tab from field to field and enter information. But
if you attempt to click on the hyperlink, you’ll discover that it doesn’t
work!

The Solution
So let’s get to work and fix the problem. The first thing you’ll need to do
is create a little VBA SubProcedure. If you’ve never written any VBA code,
not to worry. Just follow along.
Hit Alt/F11 to open
the Visual Basic Editor (VBE). Locate your test document in the
Project List and select it. Click Insert/Module to enter a code
module into the document. This is where you’ll write the VBA code. Because
you enter the code module right into the document, the code will travel
with the file if you pass it along to others.
Note! Normally you
would create a template, rather than a document, so form docs can be
created from your master form template or DOT file. But since we’re just
doing a simple sample here, we’re using a DOC file. This is fine for our
testing, but if you planned to actually use this as a form, you would want
to resave this file as a template.
Double click on the new
module to open it. Enter the following code into the module. See the image
below to make sure you’ve done it correctly.
Sub
LockedHyperlinks()
Selection.Hyperlinks(1).Follow
End Sub

Hit Ctrl/S to save
the code and then close the VBE to return to your document.
Click Tools/Unprotect
to unlock the document so you can work within it. You’ll be adding a couple
of Fields to the document. This can normally be done by clicking
Insert/Field, choosing the field, setting the options and inserting the
field into the document. But because we’ll be creating a nested field
(one field inside of another), it’s easier to do this manually. The field
bracket you’ll be entering may look like standard keyboard brackets, but
they’re not. They’re field brackets and must be entered into
the document by using Ctrl/F9.
Add a set of field brackets
into the document where you want the hyperlink to be located by hitting
Ctrl/F9. You’re going to be entering the field code for a MACROBUTTON
field. A macrobutton field is a field that allows you to create a hotspot
in a document that can be double clicked to run a macro. If you’d like to
learn more about using macrobutton fields, you might want to check out a
series of articles I recently wrote for my Ezine, TechTrax, called:
Enhancing the MacroButton Field. In these articles, I show you how to
add an envelope macrobutton to your letter template that allows you to
quickly print envelopes, as well as how to enhance the field code by using a
graphic image, rather than text.
The macro you’ll be running
when this field is clicked will be the LockedHyperlink() procedure
you just created. Enter the MACROBUTTON field code between the field
brackets and enter the name of the macro you want to run, enclosed in double
quotes. The field code should look like the field below.
{MACROBUTTON "LockedHyperlinks"}
When you create a
macrobutton field, you’d normally add the text that you want
displayed in the next position of the field as the third argument in
the code. As an example, consider this field code sample.
{MACROBUTTON "LockedHyperlinks" "Double click here to run this macro."}
However, because you’ll be
running a hyperlink, the display text will be replaced by the second
field code. While your cursor is within the first field, hit Ctrl/F9,
again, to add the second set of field brackets. Within these new
brackets, you’ll add the HYPERLINK field, along with the URL path to the web
site for the link. Your completed field code should look like the code
below.
{MACROBUTTON "LockedHyperlinks"{HYPERLINK "http://www.mousetrax.com"}}
That’s it! Now you can
select the field and hit F9 to update the field to it’s results. It
will look just like the standard hyperlink, as you first entered.
However, you’d have to double click it to activate the link since
it’s encased within a macrobutton and two clicks is the default activation
for a macrobutton.
Give it a try. Click
Tools/Protect Document/Forms. Be sure you’re connected to the
Internet! Then double click the hyperlink field and you should go to the web
URL you used within the link.
Important Note! If you are not connected to the Internet when you
attempt to click the link, you might as well go make dinner while you wait
for the timeout error to show up. Unfortunately, if you’re not connected to
the Internet, Word will take it’s sweet time attempting to locate the web
site and will, eventually, toss up an error message. But it will appear that
your system has locked up. So be sure you’re connected before you try to use
a field hyperlink.
Making the Link Work with
One Click
Since so many people are now accustomed to activating a hyperlink with a
single click, they may assume your link isn’t working if they click it once
with no results. But with a little additional VBA code, you can modify the
macro button’s default double click event and change it to work with a
single click. This way the field will act like a standard hyperlink.
Hit Alt/F11 again to
reenter the VBE. Within the same module, add the following code.
Sub AutoExec()
Application.Options.ButtonFieldClicks = 1
End Sub
Hit Ctrl/S to save
this updated module and close the VBE. Your code should now look like the
image below.

To test your document, click
Tools/Protect Document/Forms. Save and close your
document. Then reopen it. This is done to get the AutoExec
code to run when the document is first opened, and, thereby, will change the
click default event to one. If you left the original hyperlink on the page,
you’ll still see that clicking it does nothing. However, if you click on the
new field hyperlink you’ve added with the nested field codes, it will take
you to the web site.
Important Note!
If you are using a newer version of Word, you may not be able to
get the the AutoExec to accept the click event change. In that case,
rather than using AutoExec event to change the clicks, double click the
ThisDocument module and add the code to both the Document_Open and
Document_New events, as shown below.

Dian Chapman is a
Technical Consultant, Microsoft MVP, Instructor of several advanced Word
online courses, Editor of TechTrax, free support Ezine (http://www.mousetrax.com/techtrax/),
and author of the eBook: Word AutoForms and Beginning VBA.
Dian specializes in
AutoForms creating and training, technical writing, web development and tech
support. She enjoys teaching people how to enjoy their computers more and
loves the challenge of providing automated solutions to business problems.
You can find out more about Dian and read many more of her tutorials by
visiting her web site at
http://www.mousetrax.com/ and her online magazine at
http://www.mousetrax.com/techtrax/ And if you’re interested in learning
more about creating Word AutoForms or you’d like to start learning how to
use Visual Basic for Applications, Word’s programming language, be sure to
check out her new eBook at
http://www.mousetrax.com/books.html and her online classes at
http://www.mousetrax.com/techclasses.html.