Hi Amy,
What you have here is a timing problem.
- When you tab or click out of a field and the ExitText2 macro runs,
the value of strCurrBookmark in that macro is the name of the field
you're exiting from (say, "Text2"). It sets a timer (OnTime) that will
fire the GoBacktoText2 macro one second later. As soon as the
ExitText2 macro ends, the selection actually does move to the next
field.
- By the time the GoBacktoText2 macro starts to run, the selection is
already in the next field (say, "Text3"). That means the value of
strCurrBookmark now becomes "Text3", and that's what the
..Result.Select acts on.
What you need to do is store the value of strCurrBookmark from the
ExitText2 macro and use it -- not recalculate it -- in the
GoBacktoText2 macro. To do that, move the line
Dim strCurrBookmark As String
before the line Sub ExitText2() and change the keyword from Dim to
Private. This allows the value found in ExitText2 to stay in memory
until GoBacktoText2 needs it.
Remove everything from the GoBacktoText2 macro except the
ActiveDocument.Bookmarks line. Now it should look like this:
Private strCurrBookmark As String
Sub ExitText2()
If Selection.FormFields.Count = 0 And _
Selection.Bookmarks.Count > 0 Then
strCurrBookmark = _
Selection.Bookmarks(Selection.Bookmarks.Count).Name
Else
strCurrBookmark = Selection.FormFields(1).Name
End If
With ActiveDocument.FormFields(strCurrBookmark)
If Len(.Result) <= 0 Then
Application.OnTime When:=Now + TimeValue("00:00:01"), _
Name:="GoBacktoText2"
MsgBox "An entry is required for this field."
End If
End With
End Sub
Sub GoBacktoText2()
ActiveDocument.Bookmarks(strCurrBookmark).Range.Fields(1).Result.Select
End Sub