Preventing document from closing if field left blank

C

C''''est_moi

Hi to all and tks in advance. Has an article been written on the above (or a
thread maybe been answered in the past). I want a protected form not to close
(ie a user not being able to exit a document) if a specific formfield within
the document has been left blank. It is an open formfield (not a DD/not a
checkbox). I have searched a number of combinations of the above terms to no
avail.
I understand that I can write an autoclose msgbox reminder to the user but I
want to go a step further and make sure he/she cannot exit the document if
field is still blank.
 
J

Jay Freedman

Hi to all and tks in advance. Has an article been written on the above (or a
thread maybe been answered in the past). I want a protected form not to close
(ie a user not being able to exit a document) if a specific formfield within
the document has been left blank. It is an open formfield (not a DD/not a
checkbox). I have searched a number of combinations of the above terms to no
avail.
I understand that I can write an autoclose msgbox reminder to the user but I
want to go a step further and make sure he/she cannot exit the document if
field is still blank.

Hi Carmen,

You can adapt the macro shown in
http://word.mvps.org/FAQs/MacrosVBA/PseudoBeforeClose.htm to something like
this:

Public Sub AutoClose()

If Len(Trim(ActiveDocument.FormFields("Text1").Result)) = 0 Then
MsgBox "Field Text1 is empty"
ActiveDocument.Bookmarks("Text1").Range.Fields(1).Result.Select

' Then cancel closing of the document.
' Mark the document as unsaved so a prompt
' appears asking whether to save changes.
ActiveDocument.Saved = False

' Dismiss the displayed prompt.
SendKeys "{ESC}"
End If

End Sub

The Select statement will highlight the formfield (of course, substitute the
actual name of the field for "Text1"), and the SendKeys will zap the "Save?"
prompt before it even appears -- so the document won't close.

Of course, users can get quite creative when you won't let them do what they
want, so be prepared to find an assortment of nasty language in the field. :)
 
C

C''''est_moi

Thank you SOOOOO VERY MUCH Jay. This works just PER-FECT-LY fine. Let me tell
you this: I am baskin' in VBA heaven since I found this discussion group. Not
to worry, believe you me, they will all be VERY happy with this little
reminder since they often times close, print and send the form to the client
having left a blank spot instead of entering the name of the client, as they
should do. Thx a million.
 
G

Greg Maxey

Jay,

I haven't isolated the cause, but if I start out with the Word2003 Office
Assistant active and use that psuedo close code it doesn't work. If I hide
the assistant it works, and if I then reactivate the assistant it continues
to work.
 
C

C''''est_moi

Hi Greg/Jay,
Just FYI, I am in Word 2003. Double checking, I activated the Assistant,
tried again and it worked the same as before (without the Assistant active
which I never have). Since the Assistant was not active in the first place,
that may be why it continued working.

I'll see if this presents a problem to my users (I would think that at least
some of them would have the Assistant active), when I come back from vacation.
Carmen
 
J

Jay Freedman

Hi Graham,

Please correct me if I'm wrong: It seems to me that the macros in
formfieldmacros.htm won't catch an empty field if the user never enters that
field, using the mouse to click from one field to another and skipping over one
or more fields.

If you're of the belt-and-braces sort, you could use both methods in the same
template. The field-by-field checking would ensure the user fills in every field
they enter, and the AutoClose method would catch any that they skipped over.
 
G

Graham Mayor

If you apply the macros to all the fields you can save the form without
filling in anything, but you cannot leave a form field by any means if the
field is not filled.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
G

Graham Mayor

On further reflection it would be possible to leave the last field empty, if
the user did not try to leave it, so it would be worth trapping that one in
the save/print routine.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

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