I am attempting to send an email out of Access. The email is not automatically sent. I have to hit send on the email pop up.
Is there something missing in my code that is preventing the email from sending.
Dim strTo As String
Dim strMessage As String
Dim strSubject As String
strTo = "Hazat.Bangurah@umm.edu"
strSubject = "New Lab Charge Codes"
strMessage = "Attached are the New Lab Charge Codes"
DoCmd.SendObject acSendQuery, "std qry_Master to HPM Lab Standard Compare", acFormatXLSX, strTo, , , strSubject, strMessage
DoCmd.SendObject
will show a warning pop-up from outlook even if you use EditMessage := False
. So you can apply workaround to avoid it. First save the query to you disk and add that file as attachment. This work around can be done programmatically. Try below codes to send mail without any warning pop-up but you must set Programmatic Access to Never warn me about suspicious activity
. See this post from Microsoft Answer.
Private Sub CmdSendMail_Click()
Dim strTo As String
Dim strMessage As String
Dim strSubject As String
Dim attch As String
strTo = "Hazat.Bangurah@umm.edu"
attch = "D:\MyFile.xlsx"
strSubject = "New Lab Charge Codes"
strMessage = "Attached are the New Lab Charge Codes"
' Save file to disk.
DoCmd.OutputTo acOutputQuery, "Query1", acFormatXLSX, attch, False, , , acExportQualityPrint
Call SendEmailWithOutlook(strTo, strSubject, strMessage, attch)
End Sub
'======= Function to send email =======
Public Function SendEmailWithOutlook( _
MessageTo As String, _
Subject As String, _
MessageBody As String, strAttachment As String)
' Define app variable and get Outlook using the "New" keyword
Dim OutApp As Object
Dim OutMail As Object ' An Outlook Mail item
' Create a new email object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olMailItem)
' Add the To/Subject/Body to the message and display the message
With OutMail
.To = MessageTo
.Attachments.Add strAttachment
.Subject = Subject
.Body = MessageBody
.Send ' Send the message immediately
End With
' Release all object variables
Set OutApp = Nothing
Set OutMail = Nothing
End Function
To set Programmatic Access you must open outlook as Administrator. Then follow the screenshot below.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.