我使用以下代码发送电子邮件 .

手动运行时,没有问题 .

当作为Windows计划程序的一部分运行时(Excel中的宏在打开文件时自动运行),电子邮件不会被发送 . 然而,我可以保存为草稿( . 保存而不是.Send) . 我没有收到任何错误消息,计划任务正在完成,没有任何问题 .

Excel和Outlook版本:2016

事件顺序:

  • Scheduler打开Excel文件

  • 宏查询数据库(打开时)

  • 创建了单个excel文件

  • 已创建的Excel文件作为电子邮件的附件添加

  • 电子邮件发送

Sub AttachEmailStores()    
    Dim FileFormatNum As Long    
    Dim OutApp As Object
    Dim outMail As Object
    Dim Signature As String      
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With    
    Set OutApp = CreateObject("Outlook.Application")
    Set outMail = OutApp.CreateItem(0)    
            With outMail
                .to = "test@test.com"
                .BCC = ""
                .Subject = "Test"
                .Recipients.ResolveAll
                .Send
            End With            
    Set outMail = Nothing
    Set OutApp = Nothing    
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
End Sub

我正在使用库--16.0对象库 .