![]() |
|
|||||||||
| |
||||||||||
|
Training > Tutorials > Word AutoForms > Adding Hyperlinks to Locked Forms in MS Word |
||||||||||
|
|
Adding Hyperlinks to Locked Forms in MS Word
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 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 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()
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. (Note that all code should go on one line.) {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. (Note that all code should go on one line.) {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. (Note that all code should go on one line.) {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 Hit Alt/F11 again to reenter the VBE. Within the same module, add the following code. Sub AutoExec() 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 (since AutoExec is legacy code from WordBasic) . In that case, rather than using the 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. Private Sub Document_New() Private Sub Document_Open()
|
|||||||||