【发布时间】:2018-06-14 17:30:19
【问题描述】:
我正在尝试通过 VBA 发送电子邮件。
当 .Send 不起作用时,我发现 Sendkeys 方法有效,但电子邮件必须显示,然后您无法触摸计算机,否则您可能会破坏宏。
如何使 .Send 方法起作用?
Dim OutApp As Object
Dim OutMail As Object
Dim count As Integer
EmailTo = Worksheets("Email Addresses").Range("A2")
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItemFromTemplate( _
Sheets("Start Here").Range("B25"))
On Error Resume Next
With OutMail
.To = EmailTo
'.CC = ""
'.BCC = ""
'.Subject = ""
'.Body = ""
'.Attachments.Ad' ActiveWorkbook.FullName
' You can add other files by uncommenting the following line.
'.Attachments.Add ("C:\test.txt")
' In place of the following statement, you can use ".Display" to
' display the mail.
'.Display
'SendKeys "^{ENTER}" ' <---this was the fix I found when .Send didn't work
.Send
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
【问题讨论】:
-
“当 .Send 不起作用时” -
.Send以什么方式不起作用?删除On Error Resume Next并查看产生了哪些错误 - 这可能有助于使其正常工作。 -
您的电子邮件安全设置是最有可能导致问题的原因。在 Outlook>>文件选项卡>>选项>>信任中心>>信任中心设置>>程序访问。
-
根据 IT 部门的说法,根据@Sorceri 所说,您可能会也可能无法更改这些设置。例如,在我现在的工作中,我只能自动显示电子邮件,但不能通过代码发送。网络安全团队不允许这样做。
-
谢谢大家,我的信任中心设置无法更改,所以这也是这里的原因。谢谢!
-
.Display然后.Send怎么样?