How to check Required Form Fields before Saving or Printing?

B

BD

Hello,

I know that required form fields issue have been considered many
times, but I searched the Internet and I can't find the solution to my
problem.


I have few form fields in my documents. In each document there is a
different number of form fields. All form fields in each documents are
required.

Employee needs to fill all fields and then save and/or print the
document.

What I need to do.. is to check before every Saving or Printing if all
fields have been filled. If not, pop up a message with FieldName which
is missed.

How can I do this?

I'll be very grateful for all your help.

Best regards,
BD.
 
J

Jay Freedman

BD said:
Hello,

I know that required form fields issue have been considered many
times, but I searched the Internet and I can't find the solution to my
problem.


I have few form fields in my documents. In each document there is a
different number of form fields. All form fields in each documents are
required.

Employee needs to fill all fields and then save and/or print the
document.

What I need to do.. is to check before every Saving or Printing if all
fields have been filled. If not, pop up a message with FieldName which
is missed.

How can I do this?

I'll be very grateful for all your help.

Best regards,
BD.

This is a little tricky, not because it's hard to check whether there are
any blank fields (that's simple) but because Word isn't very good about
telling you when it's about to save a file. The elements of the needed parts
are in these articles:

http://word.mvps.org/FAQs/MacrosVBA/InterceptSavePrint.htm
http://word.mvps.org/fAQs/MacrosVBA/PseudoBeforeClose.htm
http://www.gmayor.com/formfieldmacros.htm

Throw this pile of code into the template of the form:

Private Function IsFieldEmpty(FF As FormField) As Boolean
IsFieldEmpty = False ' default

If FF.Type = wdFieldFormTextInput Then
If Len(Trim(FF.Result)) = 0 Then
MsgBox prompt:="Field " & FF.Name & _
" must not be blank.", Title:="Empty Field"
FF.Range.Select
IsFieldEmpty = True
End If
End If
End Function

Sub FilePrint()
Dim FrmFld As FormField
Dim StopPrint As Boolean

For Each FrmFld In ActiveDocument.FormFields
StopPrint = IsFieldEmpty(FrmFld)
If StopPrint Then Exit For
Next FrmFld

If Not StopPrint Then
Dialogs(wdDialogFilePrint).Show
End If
End Sub

Sub FilePrintDefault()
Dim FrmFld As FormField
Dim StopPrint As Boolean

For Each FrmFld In ActiveDocument.FormFields
StopPrint = IsFieldEmpty(FrmFld)
If StopPrint Then Exit For
Next FrmFld

If Not StopPrint Then
ActiveDocument.PrintOut
End If
End Sub

Sub FileSave()
Dim FrmFld As FormField
Dim StopSave As Boolean

For Each FrmFld In ActiveDocument.FormFields
StopSave = IsFieldEmpty(FrmFld)
If StopSave Then Exit For
Next FrmFld

If Not StopSave Then
ActiveDocument.Save
End If
End Sub

Sub FileSaveAs()
Dim FrmFld As FormField
Dim StopSave As Boolean

For Each FrmFld In ActiveDocument.FormFields
StopSave = IsFieldEmpty(FrmFld)
If StopSave Then Exit For
Next FrmFld

If Not StopSave Then
Dialogs(wdDialogFileSaveAs).Show
End If
End Sub

Sub AutoClose()
Dim FrmFld As FormField
Dim StopSave As Boolean

For Each FrmFld In ActiveDocument.FormFields
StopSave = IsFieldEmpty(FrmFld)
If StopSave Then Exit For
Next FrmFld

If StopSave Then
ActiveDocument.Saved = False
SendKeys "{ESC}"
End If
End Sub

The only thing missing here is a FileSaveAll macro. In Word 2003 and
earlier, you need to know to hold down the Shift key while clicking the File
menu to see this command, and in Word 2007 you have to add it to the Quick
Access Toolbar, so it's probably rarely used except by experts.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
B

BD

Hello,

Thank you. I've seen that solution. Unfortunately it's not what I
need, because it works only if you select all fields one by one. In my
case I need to avoid saving or printing document with blank fields. In
solution which you are proposing unfortunately it's possible when you
not select a field.

Do you have any other ideas?

Best regards,
BD.
 
G

Graham Mayor

The solution I posted won't let you leave a field that isn't completed.
However you cannot force users to run macros, so no solution is perfect. The
only other solution, which is subject to the same proviso, is to intercept
the save and print routines to check the validity of the data along the
lines suggested by Jay. There is no solution possible that can determine
whether the correct selection has been made from a dropdown field nor the
correct setting of a checkbox.

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


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


Hello,

Thank you. I've seen that solution. Unfortunately it's not what I
need, because it works only if you select all fields one by one. In my
case I need to avoid saving or printing document with blank fields. In
solution which you are proposing unfortunately it's possible when you
not select a field.

Do you have any other ideas?

Best regards,
BD.
 

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