发送带有Excel工作表范围的Outlook电子邮件,当计​​算机被locking时使用macros

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

系统:Windows 7 SP1,Outlook 2016,Excel 2016

问题:当我按照“运行”的设置安排任务时,无论用户是否login,excel文件都会打开并刷新,但不会通过Outlook发送邮件,也不会显示在我的发件箱中。 虽然刷新确实发生了。 这是用户没有login的时候。

当用户login时,任务计划工作正常

我已经试过这个Excel VBA – 当计算机被locking时 , 不发送电子邮件 ,它不适合我。

我用来发送邮件的function是:

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

Interesting Posts