简体   繁体   English

使用 VBA 通过 Outlook 发送的电子邮件卡在发件箱中

[英]Emails sent via Outlook using VBA stuck in outbox

I'm attempting to send out emails with attachments via Outlook (initiated on Excel).我正在尝试通过 Outlook(在 Excel 上启动)发送带有附件的电子邮件。 The code runs without error but only about 6 of the 17 emails go out, the balance are stuck in outbox and go out when I open Outlook and sync the folders myself.代码运行时没有错误,但 17 封电子邮件中只有大约 6 封发出,余额卡在发件箱中,当我打开 Outlook 并自己同步文件夹时就会出去。

I've attempted using: DoEvents and Application.Wait (Now + TimeValue("0:00:03")) to no avail.我尝试使用: DoEvents 和 Application.Wait (Now + TimeValue("0:00:03")) 无济于事。

For counter = 2 To 18

    branchCode = Workbooks("Upload.xlsm").Worksheets("Branch List").Range("C" & counter).Value

    BranchName = Workbooks("Upload.xlsm").Worksheets("Branch List").Range("A" & counter).Value
    branchEmail = Workbooks("Upload.xlsm").Worksheets("Branch List").Range("D" & counter).Value
    sheetPath = Workbooks("Upload.xlsm").Worksheets("Branch List").Range("J2").Value
    Dim OutApp As Object
    Dim OutMail As Object

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)


    On Error Resume Next

    With OutMail
        .To = branchEmail
        .BCC = ""
        .Subject = "Rate Sheet " & BranchName & " - " & Now()
        .Body = "Hi, Please find attached below your rate sheet, your uploads are ready as well."
        .Attachments.Add (sheetPath & BranchName & ".pdf")
        .Send
    End With
    On Error GoTo 0
    Application.Wait (Now + TimeValue("0:00:03"))
    Set OutMail = Nothing
    Set OutApp = Nothing

Next counter

See adjustments to code.请参阅代码调整。 Move your initialization of the Outlook application outside the loop.将 Outlook 应用程序的初始化移到循环之外。 You shouldn't be opening and closing these over and over and per your previous comment this is actually causing some issues, its possible that opening and closing the client successively is causing issues with the sync.你不应该一遍又一遍地打开和关闭这些,根据你之前的评论,这实际上会导致一些问题,连续打开和关闭客户端可能会导致同步问题。

Option 1 - Move Outlook create outside loop选项 1 - 移动 Outlook 创建外循环

Moving the initilization outside the loop MIGHT fix your issue.将初始化移到循环之外可能会解决您的问题。 If it doesn't, try option 2.如果没有,请尝试选项 2。

Option 2 - Forcibly initiate sync of "All Accounts" sync group选项 2 - 强制启动“所有帐户”同步组的同步

After all processing is complete we'll grab the sync groups using:在所有处理完成后,我们将使用以下方法获取同步组:

mySyncObjects = OutApp.GetNamespace("MAPI").SyncObjects

Then we'll kick of sync for group 1, usually "All Accounts".然后我们将启动第 1 组的同步,通常是“所有帐户”。

mySyncObjects(1).Start

If this isn't "All Accounts" you'll need to loop through mySyncObjects to find it, using the property .Name如果这不是“所有帐户”,则需要使用属性.Name遍历 mySyncObjects 以找到它

Adjusted code(note if to check for sending emails):调整后的代码(注意是否检查发送电子邮件):

'determine if you need to send emails
If needToSendEmails = 1 Then

Dim OutApp As Object
Dim OutMail As Object

Set OutApp = CreateObject("Outlook.Application")

For counter = 2 To 18



    branchCode = Workbooks("Upload.xlsm").Worksheets("Branch List").Range("C" & counter).Value

    BranchName = Workbooks("Upload.xlsm").Worksheets("Branch List").Range("A" & counter).Value
    branchEmail = Workbooks("Upload.xlsm").Worksheets("Branch List").Range("D" & counter).Value
    sheetPath = Workbooks("Upload.xlsm").Worksheets("Branch List").Range("J2").Value

    Set OutMail = OutApp.CreateItem(0)

    On Error Resume Next

    With OutMail
        .To = branchEmail
        .BCC = ""
        .Subject = "Rate Sheet " & BranchName & " - " & Now()
        .Body = "Hi, Please find attached below your rate sheet, your uploads are ready as well."
        .Attachments.Add (sheetPath & BranchName & ".pdf")
        .Send
    End With
    On Error GoTo 0
    ''This shouldn't be neccessary. I utilizie similar code to send 100+ emails quickly.  It takes a second for outlook to update but all should appear inside the app when processing complete.
    ''Application.Wait (Now + TimeValue("0:00:03")) 
    Set OutMail = Nothing


Next counter
''GET ALL SYNC GROUPS
Set mySyncObjects = OutApp.GetNamespace("MAPI").SyncObjects

''KICK OFF SYNC FOR ITEM 1 IN SYNC GROUPS, USUALLY ALL ACCOUNTS - MAY NEED TO LOOP THROUGH ALL SYNC GROUPS TO FIND "ALL ACCOUNTS"
mySyncObjects(1).Start

Set OutApp = Nothing

End If

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM