如何.Send不起作用时发送邮件?

时间:2018-01-04 22:30:43

标签: excel vba excel-vba email outlook-vba

我正在尝试通过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

1 个答案:

答案 0 :(得分:1)

您的电子邮件安全设置最有可能导致此问题。在展望&gt;&gt;文件标签&gt;&gt;选项&gt;&gt;信任中心&gt;&gt;信任中心设置&gt;&gt;程序化访问。 - Sorceri 1小时前

根据IT部门的说法,根据@Sorceri的说法,您可能会或可能无法更改这些设置。例如,在我目前的工作中,我只能自动显示电子邮件,但无法通过代码发送。网络安全团队不会允许它。 - Scott Holtzman 56分钟前

因此看起来判决是SendKeys是最好的选择,因为我的IT部门控制的安全设置已经锁定了我在信任设置中的编程访问。

感谢@Sorceri和@Scott Holtzman