How to code the ENTER key to move between form fields in a protected form in Word

Article translations Article translations
Article ID: 211219 - View products that this article applies to.
This article was previously published under Q211219
For a Microsoft Word 97 version of this article, see 187985.
Expand all | Collapse all

On This Page

SUMMARY

This article contains four Microsoft Visual Basic for Applications macros that you can use to emulate the Microsoft Word 6.0 for Windows functionality of the ENTER key for moving between form fields in protected documents.

MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs.
If you have limited programming experience, you may want to contact a Microsoft Certified Partner or Microsoft Advisory Services. For more information, visit these Microsoft Web sites:

Microsoft Certified Partners - https://partner.microsoft.com/global/30000104

Microsoft Advisory Services - http://support.microsoft.com/gp/advisoryservice

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS
For additional information about running sample code, click the article number below to view the article in the Microsoft Knowledge Base:
212536 OFF2000: How to Run Sample Code from Knowledge Base Articles

ENTER Key Functionality in Word 6.0

In Word 6.0 for Windows, if you press the ENTER key in a document that is protected for forms, the insertion point moves to the next form field. By contrast, in later versions of Word, the ENTER key does not move to the next form field but instead inserts a paragraph mark, just as it does in an unprotected document. This allows you to enter multiple lines of text into a text form field.

Macros

Following are four Visual Basic for Applications macros that you can use together to emulate the Word 6.0 for Windows functionality of the ENTER 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 ENTER 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 ENTER key, restoring the default functionality of the ENTER 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.
To unprotect the template, follow these steps:
  1. Open the template that contains the macros described in this article.
  2. On the Tools menu, click Unprotect Document.
  3. On the File menu, click Save.
  4. On the File menu, click Close.

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 EnterKeyMacro()
' Check whether the document is protected for forms
' and whether the protection is active.
   If ActiveDocument.ProtectionType = wdAllowOnlyFormFields And _
   Selection.Sections(1).ProtectedForForms = True Then
      ' Retrieve the bookmark of the current selection.
      ' This is equivalent to the name of the form field.
      myformfield = Selection.Bookmarks(1).Name
      ' Go to the next form field if the current form field
      ' is not the last one in the document.
      If ActiveDocument.FormFields(myformfield).Name <> _
      ActiveDocument.FormFields(ActiveDocument.FormFields.Count) _
      .Name Then
         ActiveDocument.FormFields(myformfield).Next.Select
      Else
         ' If the current form field is the last one,
         ' go to the first form field in the document.
         ActiveDocument.FormFields(1).Select
      End If
   Else
   ' If the document is not protected for forms,
   ' insert a tab stop character.
      Selection.TypeText Chr(13)
   End If
End Sub
				

Second Macro: Assigning the EnterkeyMacro Macro to the ENTER Key

This macro attaches the EnterKeyMacro macro to the ENTER key, thereby 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 ENTER 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 ENTER key to the EnterKeyMacro.
   KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyReturn), _
   KeyCategory:=wdKeyCategoryMacro, Command:="EnterKeyMacro"
   ' Reprotect the document with Forms protection.
   ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End Sub
				

Third Macro: Assigning the AutoOpen Macro to the ENTER 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 ENTER key when you open an existing
' Word form fields document.
   CustomizationContext = ActiveDocument.AttachedTemplate
   ' Bind the Enter key to the EnterKeyMacro.
   KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyReturn), _
   KeyCategory:=wdKeyCategoryMacro, Command:="EnterKeyMacro"
End Sub
				
NOTE: Running these macros may disable some features, such as AutoCorrect and AutoText, and may affect other features that depend on the ENTER key for proper operation. You need to run the fourth macro to restore the default functionality of the ENTER key, or restart Microsoft Word.

Fourth Macro: Removing the Command Assigned to the ENTER Key

This macro restores the default functionality of the ENTER 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
				
NOTE: The CustomizationContext property sets the location where the keyboard customization is to be saved, in this case the template attached to the active document.

REFERENCES

For additional information about available resources for creating custom solutions using Visual Basic for Applications and Microsoft Word 2000, click the article numbers below to view the articles in the Microsoft Knowledge Base:
212623 WD2000: Macro Programming Resources
226118 OFF2000: Programming Resources for Visual Basic for Applications

Properties

Article ID: 211219 - Last Review: November 23, 2006 - Revision: 2.7
APPLIES TO
  • Microsoft Word 2002
  • Microsoft Word 2000
Keywords: 
kbautomation kbmacro kbprogramming kbhowto kbinfo kbinterop KB211219

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com