即使您使用EditMessage := False,DoCmd.SendObject 也会从 Outlook 弹出警告。因此,您可以应用解决方法来避免它。首先将查询保存到您的磁盘并将该文件添加为附件。可以通过编程方式完成这项工作。尝试使用以下代码发送邮件而不弹出任何警告,但您必须将 Programmatic Access 设置为 Never warn me about suspicious activity。请参阅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
要设置程序访问,您必须以管理员身份打开 Outlook。然后按照下面的截图。