I copied and used the macro for having the enter key go to the next form
field in a form from article 211219 form the office support area. The article
mentions that if you have multiple bookmarks in the EnterkeyMacro that I may
have to add more code to handle potential errors. What would the codes be,
the macro is highlighting
myformfield = Selection.Bookmarks(1).Name
part of the macro.
Hi Mad,
The article confuses the issue a bit. The problem isn't necessarily
"multiple bookmarks"; it occurs when a form field (which has an
associated bookmark as its "name") is enclosed in one or more regular
bookmarks. In that case, when the cursor is in the form field, the
expression Selection.Bookmarks(1).Name returns the name of the
enclosing regular bookmark, not the name of the form field. Then the
macro tries to access ActiveDocument.FormFields(myformfield), which
causes an error ("The requested member of the collection does not
exist") and the macro stops.
There can also be a problem if you use copy/paste to duplicate form
fields. In that case, all the copies have no "name" bookmark. That
would cause the same error message, but on the line you quoted.
Here's a version of the macro that handles both of those situations
more gracefully. In the case of enclosing bookmarks, the For Each loop
tries every bookmark name associated with the Selection until one of
them successfully identifies the form field. The case of the nameless
field can't be worked around so easily, but at least the error message
is more informative than the one that would otherwise appear. (And in
this case the tab key still works!)
Sub EnterKeyMacro()
Dim myformfield As String
Dim bkmk As Bookmark
Dim thisFF As FormField
' 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.
For Each bkmk In Selection.Bookmarks
On Error Resume Next
myformfield = bkmk.Name
Set thisFF = ActiveDocument.FormFields(myformfield)
If Err.Number = 0 Then Exit For
Next bkmk
If Err.Number <> 0 Then
MsgBox "Bookmark " & myformfield & _
" isn't a form field.", , "Error"
Exit Sub
End If
If thisFF Is Nothing Then
MsgBox "The current form field doesn't have a name.", _
, "Error"
Exit Sub
End If
' Go to the next form field if the current form field
' is not the last one in the document.
If thisFF.Name <> _
ActiveDocument.FormFields(ActiveDocument.FormFields.Count) _
.Name Then
thisFF.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
Incidentally, the article
http://word.mvps.org/FAQs/MacrosVBA/AssignNameToFmFld.htm might be
useful to you.
--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.