簡體   English   中英

使用任務計划程序通過 VBA 發送的電子郵件卡在發件箱中

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

我有一些宏和任務計划程序可以在指定的時間啟動 Excel,更新一些表格,從這些表格創建 PDF 文檔,然后通過電子郵件發送這些 PDF 文檔以選擇個人。

有時電子郵件會卡在發件箱中,直到我打開 Outlook 才會發送。

這是發送電子郵件的代碼:

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

完成后,Private sub 跳回到此工作簿中的宏並使用 CloseWorkbook 應用程序退出 MS Excel。

我在 Outlook 的 VBA 設置中的工具參考庫:
工具參考庫設置

我的信任設置:
電子郵件安全設置

宏設置:

選擇了“啟用所有宏”

已選擇“將宏安全設置應用於已安裝的加載項”

宏設置

我們的想法是讓這個程序在清晨運行,並在他們上班時將這些電子郵件發送到選定人員的收件箱中。

Outlook 與任何其他 Office 應用程序一樣,無法在服務(例如計划程序)中運行。 也就是說,您需要強制 Outlook 執行 SendReceive 並等待它完成。 調用Namespace.SendAndReceive或從Namespace.SyncObjects集合中檢索第一個SyncObject對象,調用SyncObject.Start並等待SyncObject.SyncEnd事件觸發。

如果有人還在尋找答案; 這允許在不打開 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM