Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-09-2013, 07:49 AM
thedeadzeds thedeadzeds is offline Mandatory field Windows XP Mandatory field Office 2003
Novice
Mandatory field
 
Join Date: May 2013
Posts: 5
thedeadzeds is on a distinguished road
Default Mandatory field

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
Reply With Quote
  #2  
Old 05-09-2013, 03:44 PM
macropod's Avatar
macropod macropod is offline Mandatory field Windows 7 64bit Mandatory field Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #3  
Old 05-09-2013, 06:30 PM
fumei fumei is offline Mandatory field Windows 7 64bit Mandatory field Office XP
Expert
 
Join Date: Jan 2013
Posts: 440
fumei is on a distinguished road
Default

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:
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'
Are you saying it does not work if you put it in the ThisDocument module of the Active Document?
Reply With Quote
Reply



Similar Threads
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

Other Forums: Access Forums

All times are GMT -7. The time now is 12:51 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft