You are currently offline, waiting for your internet to reconnect

WD97: How to Code the ENTER Key to Move to Next Field in Protected Form

This article was previously published under Q187985
Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
SUMMARY
This article contains four Visual Basic for Applications macros that youcan use to emulate the Microsoft Word 6.0 for Windows functionality of theENTER key for moving between form fields in protected documents.

In Microsoft Word 6.0 for Windows, if you press the ENTER key in a documentprotected for forms, the insertion point moves to the next form field. Bycontrast, in later versions of Word, the ENTER key does not move to thenext form field but instead inserts a paragraph mark, just as it would inan unprotected document. This allows you to enter multiple lines of textinto a text form field.
MORE INFORMATION
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers 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 requirements.
What follows are the four Visual Basic for Applications macros that you canuse together to emulate the Microsoft Word 6.0 for Windows functionality ofthe ENTER key in new protected form field documents based on a template.

For this code to work as written, the template should not be protected.

The first macro moves the insertion point to the next form field. Thesecond macro assigns the first macro to the ENTER key. The third macro adds an AutoOpen macro to the ENTER key. The fourth macroremoves the assigned macro to the ENTER key, restoring the defaultfunctionality of the ENTER key.

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

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

This macro uses the Bookmarks collection to retrieve the name of thecurrent form field. The name of each form field is also the name of abookmark inserted for the form field. If you have any other bookmarks inyour document, you may have to add more code here to handle potentialerrors. The macro also assumes that all form fields in the documents allowuser input. If this is not the case in your document, you will have to addadditional code in your macro.

The macro checks whether the current section is protected or unprotected,and then either moves to the next form field (in a protected section) orinserts a paragraph mark (in an unprotected section). This functionality isnecessary for documents that contain both sections that are protected forform input and for 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: Assigns the EnterkeyMacro Macro to the ENTER Key

This macro attaches the EnterKeyMacro macro to the ENTER key, therebyreprogramming the function of the key when it is used in protected documentform fields. When you use this macro in a custom template, name it"AutoNew" (without the quotation marks). This will change the functionalityof 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: Adds an AutoOpen Macro to the ENTER Key

Add an AutoOpen macro with the following code. This ensures that the keyfunctionality will continue when you open a document based on the formtemplate 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 functions, such as AutoCorrectand AutoText, and may affect other features that depend on the ENTER keyfor proper operation. You need to run the forth macro to restore thedefault functionality of the ENTER key or restart Microsoft Word.

Fourth Macro: Removes the Command Assigned to the ENTER Key

This macro restores the default functionality of the ENTER key. When youuse 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 thekeyboard customization is to be saved, in this case the template attachedto the active document.

For additional information, please see the following article in theMicrosoft Knowledge Base:
173707 OFF97: How to Run Sample Code from Knowledge Base Articles
REFERENCES
For more information about getting help with Visual Basic for Applications,please see the following article in the Microsoft Knowledge Base:
163435 VBA: Programming Resources for Visual Basic for Applications
vb vba vbe word8 word97
Properties

Article ID: 187985 - Last Review: 01/23/2007 15:40:34 - Revision: 2.3

  • Microsoft Word 97 Standard Edition
  • kbhowto kbinfo kbinterop kbprogramming KB187985
Feedback