Sending email using VBA code - RESOLVED
I wish to send one email to txtFillOutBy via her txtEmail using Outlook.
Can anyone let me know if this will work in 2007? Current errors are undefined datatypes and data member not found. They're in red.
This is the third block of code I've found on the internet, and the simplest yet there is still pieces I don't understand. Like, at this point do I still need rst? The original code had a loop for a mass-mailing. I removed everything I could like variables for the subject and body.
Private Sub btnNotify_Click()
Dim mess_body As String
Dim rst As DAO.Recordset
Dim appOutLook As Outlook.Application
Dim MailOutLook As Outlook.MailItem
Set appOutLook = CreateObject("Outlook.Application")
Set MailOutLook = appOutLook.CreateItem(olMailItem)
Set rst = Me.RecordsetClone
If IsNull(Me.txtEmail) Then
MsgBox "skipping " & Me.txtLastName & " no email address."
GoTo skip_email
End If
Set appOutLook = CreateObject("Outlook.Application")
Set MailOutLook = appOutLook.CreateItem(olMailItem)
With MailOutLook
.To = txtEmail
.Subject = "[ACTION] " & cmbActionType.Value & " for " & txtFirstName & " " & txtLastName
.Body = "Dear " & txtFillOutBy & "," & vbCrLf & vbCrLf & cmbActionType.Value & " for " & txtFirstName & " " & txtLastName
If left(Me.Mail_Attachment_Path, 1) <> "<" Then
.Attachments.Add (Me.Mail_Attachment_Path)
End If
'next line would let MS Outlook API send the note
'without storing it in your sent bin
'.DeleteAfterSubmit = True
.Send
End With
skip_email:
rst.Close
Set rst = Nothing
End Sub
I wish to send one email to txtFillOutBy via her txtEmail using Outlook.
Can anyone let me know if this will work in 2007? Current errors are undefined datatypes and data member not found. They're in red.
This is the third block of code I've found on the internet, and the simplest yet there is still pieces I don't understand. Like, at this point do I still need rst? The original code had a loop for a mass-mailing. I removed everything I could like variables for the subject and body.
Private Sub btnNotify_Click()
Dim mess_body As String
Dim rst As DAO.Recordset
Dim appOutLook As Outlook.Application
Dim MailOutLook As Outlook.MailItem
Set appOutLook = CreateObject("Outlook.Application")
Set MailOutLook = appOutLook.CreateItem(olMailItem)
Set rst = Me.RecordsetClone
If IsNull(Me.txtEmail) Then
MsgBox "skipping " & Me.txtLastName & " no email address."
GoTo skip_email
End If
Set appOutLook = CreateObject("Outlook.Application")
Set MailOutLook = appOutLook.CreateItem(olMailItem)
With MailOutLook
.To = txtEmail
.Subject = "[ACTION] " & cmbActionType.Value & " for " & txtFirstName & " " & txtLastName
.Body = "Dear " & txtFillOutBy & "," & vbCrLf & vbCrLf & cmbActionType.Value & " for " & txtFirstName & " " & txtLastName
If left(Me.Mail_Attachment_Path, 1) <> "<" Then
.Attachments.Add (Me.Mail_Attachment_Path)
End If
'next line would let MS Outlook API send the note
'without storing it in your sent bin
'.DeleteAfterSubmit = True
.Send
End With
skip_email:
rst.Close
Set rst = Nothing
End Sub
Last edited: