Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / August 2005

Tip: Looking for answers? Try searching our database.

Previous bookmark

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Amy - 17 Aug 2005 18:47 GMT
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
Jay Freedman - 17 Aug 2005 18:53 GMT
> 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

Signature

Regards,
Jay Freedman
Microsoft Word MVP          FAQ: http://word.mvps.org

Amy - 17 Aug 2005 19:08 GMT
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?

> > 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
[quoted text clipped - 7 lines]
>
> ActiveDocument.Bookmarks(MyBookmark).Range.Select
Jay Freedman - 18 Aug 2005 01:03 GMT
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

>Thanks Jay,
>
[quoted text clipped - 13 lines]
>>
>> ActiveDocument.Bookmarks(MyBookmark).Range.Select
Amy - 18 Aug 2005 13:29 GMT
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
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.