Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can I Disable the Enter Key in a Word Form?

Status
Not open for further replies.

SilentAiche

Technical User
Dec 21, 2004
1,325
US
Folks,

Using Word 2002 on Win XP.

I have a document saved as a form in Word. The intent is for the user to tab from form field to form field and fill in the blanks. This part works fine.

The problem is that if the user hits the Enter key it inserts a "hard carriage return" (inserts a paragraph marker and moves down a line, moving everthing after the paragraph marker down a line). How can I prevent the Enter key from inserting a line? I would like the Enter key to do one of two things: 1) Nothing, or 2) Tab to the next form field.

I need a solution that applies just to my form, not to all Word docs (I know that's obvious, but I had to say it).

Word's Help didn't help, and I couldn't find this issue in a search of the forum. I did find two very helpful FAQ's by Fumei on form fields in Word, but they did not address this particular issue.

As always, your help is appreciated.

Tim
 
Well, I suppose I'm almost there. Google found the following code:

Code:
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

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

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

Sub AutoClose()
      CustomizationContext = ActiveDocument.AttachedTemplate
      FindKey(KeyCode:=BuildKeyCode(wdKeyReturn)).Disable
      ' Disables prompt to save template changes.
      Templates(1).Save
    End Sub


It does what I want with the Enter key- hitting Enter is like hitting Tab. However, the user still has to Protect the document as a Form when it first opens. I'd like to create an OnOpen event that executes a simple macro to Protect the document as a Form, but I don't know zip about Events in Word. I have the macro, but I'm not sure how to attach it to an event.

Any thoughts are welcome.

One other thing- if the macros above were inadvertently saved to Normal.dot, what is the easiest way to move them so they only apply to the Form I am working on? I'm not saying that happened, mind you, but just hypothetically...
wink.gif


THanks,
Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top