
Originally Posted by
pbaldy
Also, you could use
outMail.Display
instead of
outMail.Send
to let the user edit before sending.
The only issue with that is that they are using a loop to send a bunch of emails so they'd have to edit the content for every loop.
I've modified the code to use a dlookup per pbaldy's suggestion (untested and dlookup parameters are just placeholders):
Code:
Public Sub SendSerialEmailNextMonth()
On Error GoTo ErrHandler
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim emailTo As String
Dim emailSubject As String
Dim emailText As String
'Dim acc As Outlook.Account
Dim acc As Object
'Dim outApp As Outlook.Application
Dim outApp As Object
'Dim outMail As Outlook.MailItem
Dim outMail As Object
Dim outlookStarted As Boolean
Dim emailBody As Variant
'On Error Resume Next
Set outApp = GetObject(, "Outlook.Application")
'On Error GoTo 0
If outApp Is Nothing Then
'MsgBox "Outlook will open!!", vbOK
Shell "Outlook.exe", vbMaximizedFocus
Set outApp = CreateObject("Outlook.Application")
outlookStarted = True
End If
Set db = CurrentDb
Set rs = db.OpenRecordset("SELECT FirstName, Surname, EmailAddress, DeskVacated, VacateDesk " & _
" FROM qryPGRDesks1FilterNextMonth WHERE EmailAddress IS NOT NULL")
If Not (rs.BOF And rs.EOF) Then
rs.MoveFirst
Do Until rs.EOF
'emailTo = Trim(rs.Fields("FirstName").Value & " " & rs.Fields("Surname").Value) & _
" <" & rs.Fields("EmailAddress").Value & ">"
emailTo = Trim(rs.Fields("EmailAddress").Value)
emailSubject = "Notification to vacate desk"
If IsNull(rs.Fields("FirstName").Value) Then
emailSubject = emailSubject & " for " & _
rs.Fields("FirstName").Value & " " & rs.Fields("Surname").Value
End If
emailText = Trim("Hi " & rs.Fields("FirstName").Value) & vbCrLf & vbCrLf
'emailText = emailText & _
'"This email is to inform you that your desk needs to be vacated by " & rs.Fields("VacateDesk").Value & "." & " " & _
'"Please contact the Faculty Infrastucture Team should you need to discuss this request." & " " & _
'"Our email address is ?? <email here> " & vbCrLf & vbCrLf & _
'"Best Regards " & vbCrLf & vbCrLf & _
'"Faculty Infrastructure Team - Desk Allocation" & vbCrLf & _
'"Extension Number: ###### "
emailBody = DLookup("[FIELD NAME]", "[TABLE NAME]", "[OPTIONAL SEARCH CONDITION]")
If Not IsNull(emailBody) Then
emailText = emailText & emailBody
Else
Err.Raise 1001, , "Failed to locate email body."
End If
Set outMail = outApp.CreateItem(0)
'With outMail
'.SentOnBehalfOfName = "email here"
'End With
'Set outMail = outApp.CreateItem(olMailItem)
Set acc = GetAccountByEmail(outApp, "email here")
If Not acc Is Nothing Then
Set outMail.SendUsingAccount = acc
End If
'Set outMail = outApp.CreateItem(olMailItem)
'outMail.SendUsingAccount = GetAccountByEmail(outApp, "email here")
outMail.To = emailTo
outMail.Subject = emailSubject
outMail.Body = emailText
outMail.Send
rs.MoveNext
Loop
End If
rs.Close
ExitHandler:
Set rs = Nothing
Set db = Nothing
'If outlookStarted Then
'outApp.Quit
'End If
Set outMail = Nothing
Set outApp = Nothing
Exit Sub
ErrHandler:
MsgBox Err.Description, , Err.Number
Resume ExitHandler
End Sub