![]() |
#1
|
|||
|
|||
![]()
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. Kind regards Craig Code:
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 Last edited by macropod; 05-09-2013 at 03:39 PM. Reason: Added code tags & formatting |
#2
|
||||
|
||||
![]()
IIRC, Your event code needs to exist in the template from which the document is created. Accordingly, you should consider creating a specific template for your form.
PS: When posting code, please use the code tags. They're on the 'Go Advanced' tab.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
![]()
It also seems an odd methodology to use OnTime and a Sub to work this. Especially as you have no serious error trapping.
For example, if "Text3" - and that is an excellent name for the field - is blank, GoBacktoText3 gets fired. But what if the value is "Yogi Bear", or 2156-78923-11? I am not saying someone would put a stupid entry, but as it stands ANY entry at all is acceptable. However, be that as it may, are you wedded to trying to validate the values of the formfields on document save, rather than as each is exited. Quote:
|
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Mandatory field in a form | thedeadzeds | Word | 1 | 05-09-2013 03:27 PM |
Merge field source data field | kckay | Mail Merge | 4 | 03-25-2013 11:06 AM |
Field not showing in Pivot Table Field List | kmcovington | Excel | 0 | 10-26-2012 10:14 AM |
Can I embed a Ref field in an If field in Word 2003? | skarden | Word | 2 | 01-05-2012 03:55 PM |
Formula to subtract one month from due date field in reminder field | ghumdinger | Outlook | 1 | 10-01-2011 12:09 AM |