The following code, when placed in the ‘ThisDocument’ module, prevents the document being closed if it contains any formfields that have not been filled in.
Code:
Private Sub Document_Close()
Dim FFld As FormField, Doc As Document
Set Doc = ActiveDocument
For Each FFld In Doc.FormFields
If Trim(FFld.Result) = "" Then
MsgBox "Please complete all the items"
If Doc.Saved = True Then Doc.Reload
Exit Sub
End If
Next
End Sub
To test for whether designated formfields (in this case, “Text1” & “Text7”) have been completed, you might use code like:
Code:
Private Sub Document_Close()
Dim FFld As FormField, Doc As Document
Set Doc = ActiveDocument
For Each FFld In Doc.FormFields
Select Case FFld.Name
Case "Text1", "Text7"
If Trim(FFld.Result) = "" Then
MsgBox "Please complete the mandatory items"
If Doc.Saved = True Then Doc.Reload
Exit Sub
End If
End Select
Next
End Sub