In a protected (online) form, I would like a command to go to the next enabled form field, or even to the first form field in the document. If there is an on-exit macro for the field, I want it to fire.
I tried the following based on the Enter key substitution macro:
Private Sub Actor() ' Move to next form field so as to trigger on-exit macros but ' only if in a formfield in a protected section of the doc ' Dim sMyFormField As String 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. sMyFormField = 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(sMyFormField).Name _ ActiveDocument.FormFields(ActiveDocument.FormFields.Count) _ .Name Then ActiveDocument.FormFields sMyFormField).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 End If End Sub
——–
I get an error 5941 “The requested member of the collection does not exist.”
Debug identifies my the following If statement as the problem:
If ActiveDocument.FormFields(sMyFormField).Name _
ActiveDocument.FormFields(ActiveDocument.FormFields.Count) _
.Name Then
In the Immediate window:
? ActiveDocument.FormFields(Selection.Bookmarks(1).Name).Name
produces an error
? Selection.Bookmarks(1).Name
produces a name: CaseNo
? ActiveDocument.FormFields(ActiveDocument.FormFields.Count).Name
produces a name
Thoughts? Suggestions?
I’m also posting this on the Microsoft vba beginners newsgroup. If they come up with a solution, I’ll share it here, and vice versa.
TIA