View Single Post
 
Old 05-09-2013, 06:53 AM
thedeadzeds thedeadzeds is offline Windows XP Office 2003
Novice
 
Join Date: May 2013
Posts: 5
thedeadzeds is on a distinguished road
Default Mandatory field in a form

Hi Guys,

I have the follwoing code which works great as long as its used in 'Normal ThisDocument'. Any ideas how I get this to work in the 'Project Thisdocument'

This VBA does not allow the user to save the form unless specifc fields in a word form have any entry.

I have attached the document


Kind regards
Craig

Option Explicit
Private WithEvents oApp As Word.Application
Private Sub Document_New()
'assign Word to the application variable
If oApp Is Nothing Then
Set oApp = ThisDocument.Application
End If
End Sub
Private Sub Document_Open()
'assign Word to the application variable
If oApp Is Nothing Then
Set oApp = ThisDocument.Application
End If
End Sub
Private Sub oApp_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
If Doc.AttachedTemplate <> ThisDocument Then Exit Sub

If Doc.FormFields("Text1").Result = "" Then
Cancel = True
MsgBox "An entry is required in the Customer Name field.", , "Error"
Application.OnTime When:=Now + TimeValue("00:00:01"), Name:="GoBacktoText1"
Exit Sub
End If

If Doc.FormFields("Text2").Result = "" Then
Cancel = True
MsgBox "An entry is required in the DOB field.", , "Error"
Application.OnTime When:=Now + TimeValue("00:00:01"), Name:="GoBacktoText2"
Exit Sub
End If

If Doc.FormFields("Text3").Result = "" Then
Cancel = True
MsgBox "An entry is required in the Work Number field.", , "Error"
Application.OnTime When:=Now + TimeValue("00:00:01"), Name:="GoBacktoText3"
Exit Sub
End If

End Sub
Sub GoBacktoText1()
ActiveDocument.Bookmarks("Text1").Range.Fields(1). Result.Select
End Sub
Sub GoBacktoText2()
ActiveDocument.Bookmarks("Text2").Range.Fields(1). Result.Select
End Sub
Sub GoBacktoText3()
ActiveDocument.Bookmarks("Text3").Range.Fields(1). Result.Select
End Sub
Attached Files
File Type: doc JOTTER.doc (100.0 KB, 13 views)