infinikate
New Member
- Joined
- Nov 11, 2024
- Messages
- 2
- Office Version
- 365
- Platform
- Windows
Hey Y'all
I'm creating a form in Excel that on click sends an email, but I need it to make sure that all the fields are filled in before submitting. I'm getting the right pop-ups, but it's still writing the email if the fields are blank after I click OK on the error pop up. I'm hoping one of you will be able to help. I've never done this before and I'm reasonably confident I'm doing it the least effective way possible
I'm creating a form in Excel that on click sends an email, but I need it to make sure that all the fields are filled in before submitting. I'm getting the right pop-ups, but it's still writing the email if the fields are blank after I click OK on the error pop up. I'm hoping one of you will be able to help. I've never done this before and I'm reasonably confident I'm doing it the least effective way possible
VBA Code:
Private Sub CommandButton1_Click()
Dim xOutApp As Object
Dim xOutMail As Object
Dim xMailBody As String
On Error Resume Next
Set xOutApp = CreateObject("Outlook.Application")
Set xOutMail = xOutApp.CreateItem(0)
xMailBody = "Letter of Experience Request Form" & vbNewLine & vbNewLine & _
"Client Name: " & ClientName.Value & vbNewLine & _
"Policy Number: " & PolicyNumber.Value & vbNewLine & _
"Vehicle Details: " & Vehdesc.Value & vbNewLine & _
"Business Use Start: " & BUstart.Value & vbNewLine & _
"Business Use End: " & BUend.Value & vbNewLine & _
"Distance from Home to Office (km): " & Distance.Value & vbNewLine & _
"Send Letter to This Email: " & Email.Value & vbNewLine & _
""
Dim ErrText As String
ErrText = "Please complete following Fields..." & vbCr
If ClientName.Value = "" Then
ErrText = ErrText & vbCr & "- Client Name"
Else
End If
If PolicyNumber.Value = "" Then
ErrText = ErrText & vbCr & "- Policy Number"
Else
End If
If Vehdesc.Value = "" Then
ErrText = ErrText & vbCr & "- Vehicle Description"
Else
If BUstart.Value = "" Then
ErrText = ErrText & vbCr & "- Business Use Start Date"
Else
If BUend.Value = "" Then
ErrText = ErrText & vbCr & "- Business Use End Date"
Else
If Distance.Value = "" Then
ErrText = ErrText & vbCr & "- One Way Distance from Home to Office"
Else
If Email.Value = "" Then
ErrText = ErrText & vbCr & "- Send Letter to This Email"
Else
End If
On Error Resume Next
With xOutMail
.To = "email@email.com"
.CC = ""
.BCC = ""
.Subject = "Business Use Letter Request"
.Body = xMailBody
.Display 'or use .Send
End With
On Error GoTo 0
Set xOutMail = Nothing
Set xOutApp = Nothing
End Sub