I'm designing a userform with a textbox (txt_email_1) that the user places an email address. After searching several question forums and web pages I found one. I made some modifications to fit my needs and tried it and it seemed to work UNTIL I placed my personal email address that I've been using over 20 years and the textbox comes back saying 'Invalid Email Address'. First thing I checked was to make sure both 'Microsoft VBScript Regular Expression 5.5' and 'Microsoft Scriptlet Library' are installed and chosen. Now no matter what I place in (good or bad email addresses) I get 'Invalid Email Address'. HELP
(Placed in a module named email)
(Placed in txt_email_1 code)
(Placed in a module named email)
VBA Code:
Option Explicit
Public Function ValidateEmailAddress(ByVal strEmailAddress As String) As Boolean
On Error GoTo Catch
Dim objRegExp As New RegExp
Dim blnIsValidEmail As Boolean
objRegExp.IgnoreCase = True
objRegExp.Global = True
objRegExp.Pattern = "^([a-zA-Z0-9_\-\.]+)@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$"
blnIsValidEmail = objRegExp.Test(strEmailAddress)
ValidateEmailAddress = blnIsValidEmail
Exit Function
Catch:
ValidateEmailAddress = False
End Function
(Placed in txt_email_1 code)
VBA Code:
Private Sub Txt_email_1_Exit(ByVal cancel As MSForms.ReturnBoolean)
Dim response As Variant
If ValidateEmailAddress(txt_email_1.Text) = False Then
Me.txt_email_1.BackColor = &HC0FFFF 'yellow
response = MsgBox("That Email #1 isn't valid" & Chr(10) _
& "Click OK to re-enter or Cancel if you have no email", vbOKCancel)
If response = vbCancel Then
txt_email_1.Value = ""
Me.txt_email_1.BackColor = &H80000005 'white
Else
If response = vbOK Then
Me.txt_email_1.BackColor = &HC0FFFF 'yellow
Me.txt_email_1.SetFocus
End If
If ValidateEmailAddress(txt_email_1.Text) = True Then
Me.txt_email_1.BackColor = &H80000005 'white
End If
Exit Sub
cancel = True
End If
End If
End Sub