Enter Key in Forms

G

Greg Maxey

I received some assistance (a lot of assistance) with the following code
intented to make the enter key emulate the TAB key in a protected form. It
does a great job cycling between fields, but it turns out that "calculate on
exit" and run macors on "entry" "exit" to the fields are not functioning.
Does anyone know what addtional code is required that would emulate these
features using the enter key?

Thanks

Sub EnterKeyMacro()
' Include the following declaration in the Macro module:
'Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Dim myFF As String

If ActiveDocument.ProtectionType = wdAllowOnlyFormFields And _
Selection.Sections(1).ProtectedForForms Then
myFF = Selection.Bookmarks(1).Name

With ActiveDocument.FormFields
If .Item(myFF).Name = .Item(.Count).Name Then
.Item(1).Select
Else
.Item(myFF).Next.Select
End If
Sleep 2
myFF = Selection.Bookmarks(1).Name
Selection.GoTo What:=wdGoToBookmark, Name:=myFF
End With
Else
Selection.TypeText Chr$(13)
End If
End Sub
 
D

Doug Robbins

Hi Greg,

Try:

Dim myFF As String

If ActiveDocument.ProtectionType = wdAllowOnlyFormFields And _
Selection.Sections(1).ProtectedForForms Then
myFF = Selection.Bookmarks(1).Name

With ActiveDocument.FormFields
If .Item(myFF).Name = .Item(.Count).Name Then
.Item(1).Select
Else
If Len(.Item(myFF).Next.EntryMacro) > 0 Then
Application.Run .Item(myFF).Next.EntryMacro
End If
.Item(myFF).Next.Select
End If
Sleep 2
myFF = Selection.Bookmarks(1).Name
Selection.GoTo What:=wdGoToBookmark, Name:=myFF
End With
Else
Selection.TypeText Chr$(13)
End If
ActiveDocument.Fields.Update


I haven't tested it, but getting the Calculate on Exit to be performed
should be just a matter of including an ActiveDocument.Fields.Update in the
code.

--
Please respond to the Newsgroup for the benefit of others who may be
interested. Questions sent directly to me will only be answered on a paid
consulting basis.

Hope this helps,
Doug Robbins - Word MVP
 
G

Greg Maxey

Doug,

Thanks for the prime. I had to modify your code a bit because it would loop
from the last formfield back to the first. Here is the result. I have
probably injected some chaff, so let me know if I can clean it up.

Sub EnterKeyMacro()
' Include the following declaration in the Macro module:
'Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Dim myFF As String

If ActiveDocument.ProtectionType = wdAllowOnlyFormFields And _
Selection.Sections(1).ProtectedForForms Then
myFF = Selection.Bookmarks(1).Name

With ActiveDocument.FormFields
If Len(.Item(myFF).ExitMacro) > 0 Then
Application.Run .Item(myFF).ExitMacro
End If
If .Item(myFF).Name = .Item(.Count).Name Then
If Len(.Item(1).EntryMacro) > 0 Then
Application.Run .Item(1).EntryMacro
End If
.Item(1).Select
Else
If Len(.Item(myFF).Next.EntryMacro) > 0 Then
Application.Run .Item(myFF).Next.EntryMacro
End If
.Item(myFF).Next.Select
End If
Sleep 2
myFF = Selection.Bookmarks(1).Name
Selection.GoTo What:=wdGoToBookmark, Name:=myFF
End With
Else
Selection.TypeText Chr$(13)
End If
ActiveDocument.Fields.Update

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top