# Word Form - Required Fields



## jlizard (Oct 30, 2003)

I've created a form in Word that users fillout and print. Is there a way to make certain fields required?


----------



## Anne Troy (Feb 14, 1999)

Yes, but it requires VBA code. If the user does not enable macros, then the code doesn't run and requirement is lost anyway.

Still want it? I'll have to dig it up.


----------



## jlizard (Oct 30, 2003)

If you can find it , great. If not, no worries.

Thanks for the quick response.


----------



## Anne Troy (Feb 14, 1999)

So you WANT it? Okay! I know just who to check with.


----------



## smozgur (Oct 31, 2003)

Hi jlizard, 
Hi Queen,

jlizard,

Open VBA (Alt+F11) and find your document's VBPorject in Project Explorer (Ctrl + R) and Thisdocument class module under the Microsoft Word Objects then double click on the Thisdocument class module to open its module. And copy & paste the following code into this module displayed at right pane:


```
Dim WithEvents wordApp As Application

Private Sub Document_New()
    'In case this is a template
    Set wordApp = Application
End Sub

Private Sub Document_Open()
    Set wordApp = Application
End Sub

Private Sub wordApp_DocumentBeforePrint(ByVal Doc As Document, Cancel As Boolean)
Dim fld As FormField
    For Each fld In Doc.FormFields
        If Trim(fld.Result) = "" Then
            MsgBox "You must fill in the " & fld.Range.Bookmarks(1).Name & " field.", vbOKOnly + vbExclamation, "Error"
            Cancel = True
            fld.Select
            Exit Sub
        End If
    Next fld
End Sub
```
Now return word and save your document (also protect fields) and close & reopen it. Then try to print the document by leaving some fields blank.

I hope this helps.
Suat

(Thanks Dreamy!)


----------



## Anne Troy (Feb 14, 1999)

jlizard:

A few months ago, I'd have been lost with Suat's post. If you're lost, SAY SO.

I'll explain.


----------



## jlizard (Oct 30, 2003)

Thanks Suat, that looks like what I need.


Dreamboat, thanks for the offer. I think I've got it under control, but will definitely post if I run off course.


----------



## jlizard (Oct 30, 2003)

That worked like a charm to make sure that all the fields were filled in. I made some adjustments so that I can control what fields I want to be required.

Private Sub wordApp_DocumentBeforePrint(ByVal Doc As Document, Cancel As Boolean)
Dim fld As FormField
Dim ReqFields As New Collection

ReqFields.Add Doc.FormFields("Name")
ReqFields.Add Doc.FormFields("AccountNumber")

For Each fld In ReqFields
If Trim(fld.Result) = "" Then
MsgBox "You must fill in the " & fld.Range.Bookmarks(1).Name & " field.", vbOKOnly + vbExclamation, "Error"
Cancel = True
fld.Select
Exit Sub
End If
Next fld
End Sub


Thanks Again.


----------

