G
Greg Maxey
Hi All,
This is a bit long winded, so please stay with me. I
recently became interested in making the return key
funtion to advance between protected formfields. I found
KB article 211219 which offered a plausible solution.
However, the article specifically state that if other
bookmarks or if fillin disabled formfields were used that
additional code may be required.
Things continued to work reasonably well during testing
even after I inserted a few additional bookmarks and a few
fillin disabled formfields. The enter key would cause the
insertion point to jump to the next form field or if in
the last form field, it would jump back to the first
formfield. The only clich was that the first field
following any fillin disabled field would not
be "highlighted." The TAB key will cycle to and highlight
each field, the Enter key would cycle each field and and
highlight each field expect for fields following a fillin
disbled field.
I could figure out why these fields wouldn't highlight. I
have very limited VBA knowledge, but I tried everything I
could think of to reevaluate that field and expand the
selection over the field range. Nothing would work. I
even created another macro to run on entry to these
specific fields and then select it. I was really baffled
when the on entry macro had no effect but would work like
a charm if ran manually. Finally it dawne on me that it
could be a timing issue.
I had previosly stashed away a snippet of code for causing
a macor to "doze." for a bit. I think it was either Peter
Hewett that posted it here earlier. I applied his
Declaration statement, the Doze macro and called it in my
code below. It appears a simple 1 ms pause was all I
needed to identify the current field and select it. Now
all formfields are selected and highlighted regardless of
their location or the presence of fillin disabled fields.
Here is my code. Thrown against the wall for your
critical comments or suggestions for improvement. Thanks.
Sub EnterKeyMacro()
' Include the following declaration in the Macro module:
'Private Declare Sub Sleep Lib "kernel32" (ByVal
dwMilliseconds As Long)
Dim myFF, oFrm, oSelBkm
Set oFrm = ActiveDocument.FormFields
Set oSelBkm = Selection.Bookmarks
If ActiveDocument.ProtectionType = wdAllowOnlyFormFields
And _
Selection.Sections(1).ProtectedForForms = True Then
myFF = oSelBkm(1).Name
If oFrm(myFF).Name <> oFrm(oFrm.Count).Name Then
oFrm(myFF).Next.Select
'I added these three lines.
Doze 1
myFF = oSelBkm(1).Name
Selection.GoTo What:=wdGoToBookmark, Name:=myFF
Else
oFrm(1).Select
End If
Else
Selection.TypeText Chr(13)
End If
End Sub
Sub Doze(ByVal lngPeriod As Long)
DoEvents
Sleep lngPeriod
' Call it in desired location to sleep for 1 second like
this:
' Doze 1000
End Sub
This is a bit long winded, so please stay with me. I
recently became interested in making the return key
funtion to advance between protected formfields. I found
KB article 211219 which offered a plausible solution.
However, the article specifically state that if other
bookmarks or if fillin disabled formfields were used that
additional code may be required.
Things continued to work reasonably well during testing
even after I inserted a few additional bookmarks and a few
fillin disabled formfields. The enter key would cause the
insertion point to jump to the next form field or if in
the last form field, it would jump back to the first
formfield. The only clich was that the first field
following any fillin disabled field would not
be "highlighted." The TAB key will cycle to and highlight
each field, the Enter key would cycle each field and and
highlight each field expect for fields following a fillin
disbled field.
I could figure out why these fields wouldn't highlight. I
have very limited VBA knowledge, but I tried everything I
could think of to reevaluate that field and expand the
selection over the field range. Nothing would work. I
even created another macro to run on entry to these
specific fields and then select it. I was really baffled
when the on entry macro had no effect but would work like
a charm if ran manually. Finally it dawne on me that it
could be a timing issue.
I had previosly stashed away a snippet of code for causing
a macor to "doze." for a bit. I think it was either Peter
Hewett that posted it here earlier. I applied his
Declaration statement, the Doze macro and called it in my
code below. It appears a simple 1 ms pause was all I
needed to identify the current field and select it. Now
all formfields are selected and highlighted regardless of
their location or the presence of fillin disabled fields.
Here is my code. Thrown against the wall for your
critical comments or suggestions for improvement. Thanks.
Sub EnterKeyMacro()
' Include the following declaration in the Macro module:
'Private Declare Sub Sleep Lib "kernel32" (ByVal
dwMilliseconds As Long)
Dim myFF, oFrm, oSelBkm
Set oFrm = ActiveDocument.FormFields
Set oSelBkm = Selection.Bookmarks
If ActiveDocument.ProtectionType = wdAllowOnlyFormFields
And _
Selection.Sections(1).ProtectedForForms = True Then
myFF = oSelBkm(1).Name
If oFrm(myFF).Name <> oFrm(oFrm.Count).Name Then
oFrm(myFF).Next.Select
'I added these three lines.
Doze 1
myFF = oSelBkm(1).Name
Selection.GoTo What:=wdGoToBookmark, Name:=myFF
Else
oFrm(1).Select
End If
Else
Selection.TypeText Chr(13)
End If
End Sub
Sub Doze(ByVal lngPeriod As Long)
DoEvents
Sleep lngPeriod
' Call it in desired location to sleep for 1 second like
this:
' Doze 1000
End Sub