Previous bookmark

A

Amy

I'm using code to check to make sure an entry was made in a bookmarked field.
After the message box prompts the user that the field requires an entry, how
do I get back to the original bookmark, so that the user cannot go any
further until an entry is made?

Thanks. Amy
 
J

Jay Freedman

Amy said:
I'm using code to check to make sure an entry was made in a
bookmarked field. After the message box prompts the user that the
field requires an entry, how do I get back to the original bookmark,
so that the user cannot go any further until an entry is made?

Thanks. Amy

Assuming you have the name of the bookmark in a string variable named
MyBookmark, write

ActiveDocument.Bookmarks(MyBookmark).Range.Select
 
A

Amy

Thanks Jay,

I've got about 20 bookmarks. Since I want to keep sending the user back to
the empty bookmark until it is filled, is there anyway to just cycle through
them without having to hard code each bookmark name?
 
J

Jay Freedman

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?
 
A

Amy

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
 
J

Jay Freedman

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
 
A

Amy

Thank you Jay and everyone who assist those of us that research and copy code
without a clue on how to use it!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top