That depends on a number of factors...
- Are these really bookmarks (as in Insert > Bookmark) or form fields
in a protected form?
- What is the "trigger" -- event or user action -- that causes the
macro to run?
- Do the bookmark names have some relationship that would be easy to
calculate, or are they just convenient descriptive terms?
- Do the bookmarks have to be filled in a particular order, or is this
just an overall check at the end of the session?
Less critical to the design, but important to think about: What should
the user do if they don't know what to put in a bookmark? Is there an
"escape hatch" to let them out of this endless cycle?
--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
They are form fields in a protected form with a descriptive bookmark name
attached and the fields are entered in a particular order. Currently, I have
the macro set up to run when the user exits the field. I had considered just
doing a check when the user completes the document, just didn't know how I
would know, they may decide to email the document rather than save and print.
As for an endless cycle, the person I'm creating this document for just
wants to make sure every required field is complete - I hadn't thought about
it. But you are right, some of the people I work with would call me to
figure out why it keeps cycling thru and why they can't leave it empty!
I'm currently using the following code from the MVP sight:
Sub ExitText2()
Dim strCurrBookmark As String
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()
Dim strCurrBookmark As String
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
ActiveDocument.Bookmarks(strCurrBookmark).Range.Fields(1).Result.Select
End Sub
I get the message box but then it skips to the next field.
Thanks for your help!
Amy
> That depends on a number of factors...
>
[quoted text clipped - 36 lines]
> >>
> >> ActiveDocument.Bookmarks(MyBookmark).Range.Select
Jay Freedman - 19 Aug 2005 01:57 GMT
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
--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
>They are form fields in a protected form with a descriptive bookmark name
>attached and the fields are entered in a particular order. Currently, I have
[quoted text clipped - 88 lines]
>> >>
>> >> ActiveDocument.Bookmarks(MyBookmark).Range.Select
Amy - 22 Aug 2005 13:59 GMT
Thank you Jay and everyone who assist those of us that research and copy code
without a clue on how to use it!
> Hi Amy,
>
[quoted text clipped - 147 lines]
> >> >>
> >> >> ActiveDocument.Bookmarks(MyBookmark).Range.Select