VBA 宏在任务计划程序中“用户是否登录”时无法发送 outlook 邮件

VBA macro fails to send outlook mail when' user is logged on or not' in task scheduler

与 excel 的交互有效,但无法与 outlook 交互

我可以在任务调度程序中以“用户是否登录”模式安排以下任务,它是工作正常。 (运行ning 来自 VBS 文件)

Sub runTaskTest()
   Dim erow As Long
    erow = Sheets(1).Cells(Rows.Count, "A").End(xlUp).Row
    Sheets(1).Cells(erow + 1, 1).Value = "This test was successful : " & Now
    ThisWorkbook.Saved = True
    ThisWorkbook.Save
End Sub

但是当我尝试使用 excel [=70] 从 outlook 发送自动 电子邮件 =] 宏 调度程序 失败 到 运行 选项“用户是否登录”。 =16=]

Sub runTaskTest()
    
  Dim OutlookApp As Outlook.Application
  Dim OutlookMail As Outlook.MailItem

  Set OutlookApp = New Outlook.Application
  Set OutlookMail = OutlookApp.CreateItem(olMailItem)
  
  With OutlookMail
    .BodyFormat = olFormatHTML
    .Display
    .HTMLBody = "Dear ABC" & "<br>" & "<br>" & "Please find the attached file" & 
    .HTMLBody
    'last .HTMLBody includes signature from the outlook.
''<br> includes line breaks
 b/w two lines
    .To = "abc@gmail.com"
    .Subject = "Test mail"
    .Attachments = ThisWorkbook
    .Send
  End With

End Sub

VBScript

Option Explicit  
Dim xlApp
Dim xlBook

Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("D:\Excel_Test\excel_test.xlsm", 0, False)

xlApp.DisplayAlerts = False
xlApp.Visible = False

xlApp.Run "'excel_test.xlsm'!runTaskTest" // **
xlBook.Saved = True
xlBook.Save

xlBook.Close
xlApp.Quit

Set xlBook = Nothing
Set xlApp = Nothing

但是当我 select " 运行 只有当用户登录"

时,相同的代码才能正常工作

我尝试了以下解决方案,但没有任何帮助

  1. 在引用的“C:\Windows\System32\config\systemprofile\Desktop”中创建了空文件夹

  2. How to Send Email When Computer is Locked?

  3. 检查了 运行 任务的所有特权和权限(作为批处理作业登录)

但没有任何帮助。请帮助我解决问题。

如果您只处理 Exchange 帐户,请考虑使用 EWS,有关详细信息,请参阅 Explore the EWS Managed API, EWS, and web services in Exchange

Considerations for server-side Automation of Office 文章是这样说的:

Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution.

您还可以考虑使用来自 windows 服务或任务 运行 的 Outlook 基于(扩展 MAPI)的低级 API Windows 调度程序。例如,您可以考虑使用构建在 API 之上的任何第三方组件,例如 Redemption。