PDA

View Full Version : Disable Enter key in a word form



Pino79
10-11-2007, 05:54 AM
Hi
Sorry for my bad English.
I need to disable enter key in a word form.
I found some VBA Code on a Microsoft website:

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
This code works great. the problem is, the enter key is disable for all word documents and not only for that with the form.
Can someone help me?
Thanks in advance.
Marcel

fumei
10-11-2007, 09:25 AM
You are using this with Normal.dot which means it applies for all documents.

You should be using this with another template.

BTW:

" ' If the document is not protected for forms,
' insert a tab stop character.
Selection.TypeText Chr(13) "

Chr(13) is not a tab stop character. It is a paragraph mark.

Pino79
10-12-2007, 12:59 AM
Thank you for reply.
I've tried to change templates but never changed.

Bye

Pino79
10-12-2007, 03:29 AM
Ok ok sorry, I've resolved the problem with a new template.
Thank you fumei.
Ciao