MORE INFORMATION
If you want a hyperlink to work in a document that has been protected for forms, you must nest your
HYPERLINK field in a
MacroButton field. The
MacroButton field must then call a macro that follows the hyperlink.
To enable a hyperlink to work in a document that is protected for forms, follow these steps:
- Open a Word document.
- Use the following Visual Basic for Applications (VBA) code to create a macro that is named FollowLink:
Sub FollowLink()
Selection.Hyperlinks(1).Follow
End Sub
- Insert a hyperlink in the document when the form is unprotected.
- Select the hyperlink text, and then press ALT+F9 to display the HYPERLINK field code. You will see the HYPERLINK field code that is similar to the following example code:
{HYPERLINK "http://www.mvps.org/word/"}
- Select the HYPERLINK field code, and then press CTRL+F9. You will see the HYPERLINK field code text that is similar to the following example code:
{ {HYPERLINK "http://www.mvps.org/word/"} }
- Modify the HYPERLINK field code that is described in step 5 as follows:
{ MacroButton "FollowLink"{ HYPERLINK "http://www.mvps.org/word/" }}
Note If you do not want the result to display a space after the URL, remove any spaces before and after the nested field. - Select the MacroButton field code, press ALT+F9, and then press F9 to update the field and to display the hyperlink text.
- Double-click the hyperlink to open the Web site.
Note By default, you must double-click the hyperlink to open the Web page. If you want the Web page to open when you single-click the hyperlink, you can use the following VBA code to create the AutoExec macro on the global template (Normal.dot):
Sub AutoExec
Options.ButtonFieldClicks = 1
End Sub
This macro will change the behavior of all MacroButton fields in all documents. Therefore, the Web pages to which all hyperlinks point will be opened when you single-click the hyperlinks.
If you want the
MacroButton fields in documents that are based on certain templates to be followed when you single-click hyperlinks in some documents and when you double-click hyperlinks in other documents, you can use the
DocumentChange event procedure. The
DocumentChange event procedure contains the following code:
Private Sub oApp_DocumentChange()
If ActiveDocument.AttachedTemplate.Name = _
"MyTemplate.Dot" Then
Options.ButtonFieldClicks = 1
Else
Options.ButtonFieldClicks = 2
End If
End Sub
When you click a hyperlink, Word does not determine whether you are online before it tries to follow the hyperlink. If you are not connected to the Internet, Word does not prompt you to connect. Instead, Word continues to try unsuccessfully to follow the hyperlink before Word times out. You will experience this same issue when you nest your
HYPERLINK field in a
MacroButton field. This issue occurs because when you click an ordinary hyperlink in Word, you can continue to work while Word tries to follow the hyperlink. However, if you use a macro to follow a hyperlink, you cannot work until Word either opens the Web page to which the hyperlink points or times out. This issue occurs even if the following conditions are true:
- The Web page that you want to open by using the hyperlink is in the Web browser cache.
- You have made that Web page available offline.
You cannot use VBA code to verify whether a computer is connected to the Internet. However, you can use the following code to modify the FollowLink macro:
Sub FollowLink()
Dim Result As Long
Result = MsgBox("Are you definitely connected to the Internet?", _
vbYesNo + vbQuestion)
If Result = vbYes Then
Selection.Hyperlinks(1).Follow
Else
MsgBox "Please try again later", vbInformation
End If
The macro will run when you click a hyperlink. Then, you will receive a message that prompts you whether you are connected to the Internet. If you click
Yes, Word will try to open the Web page to which the hyperlink points. If you click
No, you will receive the following message:Please try again later.