Using hyperlinks in protected forms
Article contributed by Cindy Meister and Dave Rado
The basics
If you need to have a clickable hyperlink in a protected area of a document that has been protected for forms, then you have to nest your Hyperlink field within a MacroButton field, and have the MacroButton field call a macro. A single macro can be used for every hyperlink.
The macro you need is as follows:
Sub FollowLink()
Selection.Hyperlinks(1).Follow
End Sub
Although see the warning below).
Having created your macro, insert your hyperlink in the form while it's unprotected, and press Alt+F9 to display the field codes. You'll see something like:
{ HYPERLINK "https://wordmvp.com/" }
Select the field, and press Ctrl+F9, which will nest the hyperlink field inside another field, as follows:
{ { HYPERLINK "https://wordmvp.com/" } }
Then edit the field as follows:
{ MacroButton "FollowLink"{ HYPERLINK "https://wordmvp.com/" }}
Note that, if you don't want the field result to display a space after the url, you need to remove any spaces before and after the nested field, as shown above,
Press Alt+F9 again to turn the display of field codes off, update the field (F9), protect your form and you're done.
Double-click or single-click
However, by default, the user will need to double-click the MacroButton field to follow the hyperlink, rather than being able to single-click it. You can change that behaviour by putting the following code into the AutoExec macro of a Global template
Sub AutoExec
Options.ButtonFieldClicks = 1
End Sub
But doing that will change the behaviour of all MacroButton fields in all documents (they will always be activated by a single-click). If you want the MacroButton fields in documents based on certain templates to be activated by a single-click, and in other documents to be activated by a double-click, you would have to use a DocumentChange event procedure:
Private Sub oApp_DocumentChange()
If
ActiveDocument.AttachedTemplate.Name = _
"MyTemplate.Dot"
Then
Options.ButtonFieldClicks = 1
Else
Options.ButtonFieldClicks = 2
End If
End Sub
Warning!
A general point about hyperlinking to web pages from Word: be aware that Word doesn't check whether you're online before trying to follow a hyperlink; and if you're not connected to the internet, it doesn't give you the option to dial up. Instead, it takes forever trying unsuccessfully to follow the hyperlink, before timing out.
This problem is compounded if you nest your Hyperlink field within a MacroButton field; because whereas if you click on a normal hyperlink in Word, you can continue to work while Word attempts to open the hyperlink, if you use a macro to follow a hyperlink, you will be unable to work until Word has either opened the link or timed out. By then many users will have rebooted in despair!
Worse still, it fails even if the web page you are hyperlinking to is in your browser's cache, and even if you have made that page available off-line!! If not a bug, this is certainly a nasty feature and well worth contacting http://support.microsoft.com/contactus/?ws=support about.
VBA does not offer any way around this (you can't use VBA to test whether you're connected)). The best you can do is:
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
End Sub