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.