简体   繁体   English

使用任务计划程序通过 VBA 发送的电子邮件卡在发件箱中

[英]Email sent with VBA using Task Scheduler gets stuck in Outbox

I have some macros and Task Scheduler to launch Excel at a specified time, update some tables, create PDF documents from those tables and then email those PDF documents to select individuals.我有一些宏和任务计划程序可以在指定的时间启动 Excel,更新一些表格,从这些表格创建 PDF 文档,然后通过电子邮件发送这些 PDF 文档以选择个人。

Sometimes the email gets stuck in the Outbox and does not send until I open up Outlook.有时电子邮件会卡在发件箱中,直到我打开 Outlook 才会发送。

Here is the code for sending the email:这是发送电子邮件的代码:

Option Explicit

Public strFileName As String

Sub EmailPDFAsAttachment()
'This macro grabs the file path and stores as a concatenation/variable. Then it emails the file to whomever you specify.
' Works in Excel 2000, Excel 2002, Excel 2003, Excel 2007, Excel 2010, Outlook 2000, Outlook 2002, Outlook 2003, Outlook 2007, Outlook 2010.
' This example sends the last saved version of the Activeworkbook object .

    Dim OutApp As Object
    Dim OutMail As Object
    Dim FilePath As String

    'This part is setting the strings and objects to be files to grab with their associated filepath. (e.g. FilePath is setting itself equal to the text where we plan to set up each report)

    FilePath = "\\"ServerNameHere"\UserFolders\_AutoRep\DA\PDFs\SealantsVS1SurfaceRestore\" _
    & strFileName & ".pdf"

    With Application
        .EnableEvents = True
        .ScreenUpdating = True
  '  End With

    'Below is where it creats the actual email and opens up outlook.
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    On Error Resume Next
   ' ******Make sure to set the .To to only recipients that are required to view it. Separate email addresses with a semicolon (;).
   ' Current distribution list:
   ' 

    With OutMail
        .To = "example@Example.com"
        .CC = ""
        .BCC = ""
        .Subject = strFileName

        .HTMLBody = "Hello all!" & "<br>" & _
        "Here is this month's report for the Sealants vs Surface Restore. It goes as granular as to by show results by provider." & "<br>" & _
         "Let me know what you think or any comments or questions you have!" & "<br>" & _
         vbNewLine & .HTMLBody
         'Here it attached the file, saves the email as a draft, and then sends the file if everything checks out.
        .Attachments.Add FilePath
        .Send

    End With
    On Error GoTo 0

   ' With Application
   '    .EnableEvents = True
   '   .ScreenUpdating = True
    End With
'This closes out the Outlook application.
    Set OutMail = Nothing
    Set OutApp = Nothing

End Sub

After this completes, the Private sub jumps back to the macros in this workbook and quits MS Excel with the CloseWorkbook Application.完成后,Private sub 跳回到此工作簿中的宏并使用 CloseWorkbook 应用程序退出 MS Excel。

My tools reference library in Outlook's VBA settings:我在 Outlook 的 VBA 设置中的工具参考库:
工具参考库设置

My Trust Settings:我的信任设置:
电子邮件安全设置

Macro Settings:宏设置:

"Enable all macros" selected选择了“启用所有宏”

"Apply macro security settings to installed add-ins" selected已选择“将宏安全设置应用于已安装的加载项”

宏设置

The idea is to have this program run in the early morning and have these emails in the inbox of select individuals by the time they come in to work.我们的想法是让这个程序在清晨运行,并在他们上班时将这些电子邮件发送到选定人员的收件箱中。

Outlook, just like any other Office app, cannot run in a service(such as the Scheduler). Outlook 与任何其他 Office 应用程序一样,无法在服务(例如计划程序)中运行。 That being said, you need to force Outlook to perform SendReceive and wait for it to complete.也就是说,您需要强制 Outlook 执行 SendReceive 并等待它完成。 Call Namespace.SendAndReceive or retrieve the first SyncObject object from the Namespace.SyncObjects collection, call SyncObject.Start and wait fro the SyncObject.SyncEnd event to fire.调用Namespace.SendAndReceive或从Namespace.SyncObjects集合中检索第一个SyncObject对象,调用SyncObject.Start并等待SyncObject.SyncEnd事件触发。

If anyone is still looking for an answer;如果有人还在寻找答案; this allows to actually send an email without opening outlook app.这允许在不打开 Outlook 应用程序的情况下实际发送电子邮件。

Dim mySyncObjects As Outlook.SyncObjects
Dim syc As Outlook.SyncObject
Set mySyncObjects = Outlook.Application.GetNamespace("MAPI").SyncObjects
Set syc = mySyncObjects(1)
syc.start

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 VBA 通过 Outlook 发送的电子邮件卡在发件箱中 - Emails sent via Outlook using VBA stuck in outbox Excel VBA 代码在使用任务计划程序发送 Outlook 电子邮件时引发运行时错误 429 - Excel VBA code throws run-time error 429 while sending Outlook email using Task Scheduler 在 Windows 任务计划程序中安排时如何通过 VBA 发送电子邮件? - How to send email through VBA when scheduled in Windows Task Scheduler? 如何在任务调度程序中使用 VBA 正确运行 powershell 脚本? - How to properly run a powershell script using VBA in a task scheduler? 使用任务计划程序时访问 VBA 在服务器上找不到 Excel 文件 - Access VBA not finding Excel files on server when using Task Scheduler 使用任务计划程序从 PowerShell 创建电子邮件(运行时错误 429) - Create email from PowerShell using Task Scheduler (Runtime Error 429) Outlook 和 Excel VBA 任务计划程序 - Outlook and Excel VBA task Scheduler Excel VBA:答案“卡住” - Excel VBA: Answer gets "stuck" 任务计划程序不运行 Excel VBA 代码以将 PDF 作为电子邮件附件发送 - Task Scheduler does not run Excel VBA Code to send PDF as Email Attachment 如何更改使用vba发送给gmail的电子邮件中的.from? - how to change the .from on the email I sent using vba for gmail?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM