PDA

View Full Version : Required Fields in Word online/protected/e-form



Rightasrainy
10-10-2012, 08:33 AM
Hello,

I am creating a form which requires mandatory fields to be inputted, so far I have this code:

Option Explicit
Dim m_FF As FormField
Public Sub FFOnExit()
Set m_FF = GetCurrentFF
With m_FF
If Len(Trim(.Result)) = 0 Then
MsgBox "Please enter a " & .Name & " "
Application.OnTime When:=DateAdd("s", 0.1, Now), Name:="GoBacktoFF"
End If
End With
End Sub
Private Function GetCurrentFF() As Word.FormField
With Selection
If .FormFields.Count = 1 Then
Set GetCurrentFF = .FormFields(1)
ElseIf .FormFields.Count = 0 And .Bookmarks.Count > 0 Then
Set GetCurrentFF = ActiveDocument.FormFields(.Bookmarks(.Bookmarks.Count).Name)
End If
End With
End Function
Sub GoBacktoFF()
ActiveDocument.Bookmarks(m_FF.Name).Range.Fields(1).Result.Select
End Sub

Private Sub CommandButton2_Click()
MsgBox "Your email has been sent"
Dim olApp As Object
Dim olMsg As Object

Set olApp = CreateObject("Outlook.Application")
Set olMsg = olApp.CreateItem(0)
With olMsg
.To = "mtcuk.it@morganplc.com"
.Subject = "New User"

.Attachments.Add Me.Path + "\" + Me.Name

.Send

End With
Set olMsg = Nothing
Set olApp = Nothing
End Sub

This works fine if the tab button is used, however, if you use your cursor you are able to bypass certain fields completely.

Is there a way that the next field is automatically selected (like tabbing the fields would), or for an error message to be given if these fields are entered when submitting the email?

Thanks in advance.

Lorraine