Protecting a WORD form

PaulO

Registered User.
Local time
Today, 06:30
Joined
Oct 9, 2008
Messages
421
Hi

I've developed a form document in WORD with text fields and drop-down lists and help text boxes etc and all works fine ...

EXCEPT ...

... should the user type some text in a field and then press ENTER as opposed to TAB an extra row/space is created, which I need to prevent from happening as the form then expands onto a second page and/or looks completely naff!

So ... can I disable the ENTER button/command, or is there another technique I could use to ensure that fields are not expanded and that extra rows cannot be created?

My end goal is to put this form up onto the Internet for users to complete and print online, or download and complete/print.
 
I knew I had something like this hanging around somewhere. I haven't used it for a very long time but I did find it originallly on the MS Word Website. Basically it's a series of four macros that are attached to your first form field. I've just copied the entire text as I had it archived, hope it helps.

Following are four Visual Basic for Applications macros that you can use together to emulate the Word 6.0 for Windows functionality of the RETURN key in new protected form field documents based on a template. The following is a brief description of the functionality of each macro:

The first macro moves the insertion point to the next form field.
The second macro assigns the first macro to the RETURN key.
The third macro ensures that the key functionality continues when you open a document based on the form template in the future.
The fourth macro removes the assigned macro from the RETURN key, restoring the default functionality of the RETURN key.

IMPORTANT: For this code to work as written, the template should not be protected. If the template is protected, you receive the following error message:
The context cannot be modified.

First Macro: Moving the Insertion Point to the Next Form Field

This macro moves the insertion point to the next form field. If the current form field is the last one in the document, it moves the insertion point to the first form field.

This macro uses the Bookmarks collection to retrieve the name of the current form field. The name of each form field is also the name of a bookmark inserted for the form field. If you have any other bookmarks in your document, you may have to add more code here to handle potential errors. The macro also assumes that all form fields in the documents allow user input. If this is not the case in your document, you need to add additional code in your macro.

The macro checks to see whether the current section is protected or unprotected and then either moves to the next form field (in a protected section) or inserts a paragraph mark (in an unprotected section). This functionality is necessary for documents that contain both sections that are protected for form input and unprotected sections.


Sub NextFieldMacro()
' See if the document is protected and if the protection is currently active

If ActiveDocument.ProtectionType = wdAllowOnlyFormFields And _
Selection.Sections(1).ProtectedForForms = True Then
' Get the bookmark of the current selection. The bookmark is the name of the form field
myformfield = Selection.Bookmarks(1).Name
' Check to see if this is the last form field, if it’s not then go to the next one
If ActiveDocument.FormFields(myformfield).Name <> _
ActiveDocument.FormFields(ActiveDocument.FormFields.Count) _
.Name Then
ActiveDocument.FormFields(myformfield).Next.Select
Else
' Check to see if this is the last form field, if it is then return to the first field in the document
ActiveDocument.FormFields(1).Select
End If
Else
' insert a tab stop character.
Selection.TypeText Chr(13)
End If
End Sub

Second Macro: Assigns the NextFieldMacro Macro to the RETURN key
This macro attaches the NextFieldMacro macro to the RETURN key, reprogramming the function of the key when it is used in protected document form fields. When you use this macro in a custom template, name it AutoNew. This changes the functionality of the RETURN key in all new form documents based on the template.
Sub AutoNew()
' Do Not protect the template containing these macros.
CustomizationContext = ActiveDocument.AttachedTemplate
' Bind the RETURN key to the NextFieldMacro.
KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyReturn), _
KeyCategory:=wdKeyCategoryMacro, Command:="NextFieldMacro"
' Reprotect the document with Forms protection.
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End Sub

Third Macro: Assigning the AutoOpen Macro to the RETURN key
Add an AutoOpen macro with the following code. This ensures that the key functionality continues when you open a document based on the form template in the future.
Sub AutoOpen()
' This macro will reassign the RETURN key when you open an existing
' Word form fields document.
CustomizationContext = ActiveDocument.AttachedTemplate
' Bind the RETURN key to the NextFieldMacro.
KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyReturn), _
KeyCategory:=wdKeyCategoryMacro, Command:="NextFieldMacro"
End Sub

NOTE: Running these macros may disable some features, such as AutoCorrect and AutoText, and may affect other features that depend on the RETURN key for proper operation. You need to run the fourth macro to restore the default functionality of the RETURN key, or restart Microsoft Word.
Fourth Macro: Removing the Command Assigned to the RETURN key
This macro restores the default functionality of the RETURN key. When you use this macro in a custom template, name it AutoClose.
Sub AutoClose()
CustomizationContext = ActiveDocument.AttachedTemplate
FindKey(KeyCode:=BuildKeyCode(wdKeyReturn)).Disable
' Disables prompt to save template changes.
Templates(1).Save
End Sub
 

Users who are viewing this thread

Back
Top Bottom