计算机锁定时发送 Outlook 电子邮件

Sending an Outlook email when the Computer is locked

我想在计算机锁定时发送范围为 Excel sheet 的 Outlook 电子邮件

我是 运行 每周刷新一次的仪表板,使用 ODBC 连接。我写了一个在 auto_open 上运行的宏。文件由任务计划程序打开。

系统: Windows 7 SP1, 展望2016, Excel 2016

问题:当我使用 运行 设置安排任务时,无论用户是否登录,Excel 文件打开并刷新,但它不发送邮件,也不发送邮件它出现在我的发件箱中吗?刷新确实成功发生了。 这是用户未登录的时候。我的意思是计算机被锁定了。

用户登录后任务计划工作正常

我试过这个 Excel VBA - Email Does not Send When Computer is Locked 但它对我不起作用。

我用来发送邮件的函数是:

Dim oApp As Object, OutApp As Object, OutMail As Object
Dim rng As Range
Dim strbody As String, strtail As String

strbody = "Hi team," & "<br>" & _
         "<a href=""https://example.com"">Here</a> is the link to cloud upload" & Worksheets("Core View").Range("M2") & "<br><br>"
strtail = "Thanks," & "<br>" & _
"Team." & "<br><br>"

On Error Resume Next
'Only the visible cells in the selection
'Set rng = Selection.SpecialCells(xlCellTypeVisible)
Set rng = Sheets("Core View").Range("A7:K106").SpecialCells(xlCellTypeVisible)
On Error GoTo 0


If rng Is Nothing Then
    MsgBox "The selection is not a range or the sheet is protected. " & _
      vbNewLine & "Please correct and try again.", vbOKOnly
End If

With Application
    .EnableEvents = False
    .ScreenUpdating = False
End With


'Create the mail
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

On Error Resume Next
With OutMail
    .To = "plaknas@example.com"
    .CC = ""
    .BCC = ""
    If EmptySheets <> "" Then
        .Subject = "update has issues in " & EmptySheets
    Else
        .Subject = "Update for week" & Worksheets("Core View").Range("M2")
    End If
    .HTMLBody = strbody & RangetoHTML(rng) & strtail
    .Send   'or use .Display
End With
On Error GoTo 0
With Application
    .EnableEvents = True
    .ScreenUpdating = True
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Function

您不能在任务计划程序 运行 的脚本或程序中或作为 Windows 服务使用 Outlook 对象模型。安全上下文完全不同,代码不会按预期 运行:

https://support.microsoft.com/en-us/kb/237913