VBA发送批量电子邮件性能问题

时间:2018-07-03 13:47:54

标签: excel-vba performance outlook outlook-vba vba

我有以下宏,该宏将扫描B列中带有经理电子邮件的excel文件。对于每位经理,将起草一封电子邮件/附加一个excel文件并自动发送。我已经能够对此进行测试,并且在起草50-100时效果很好。

我担心的是, 50-100 电子邮件似乎并不能很好地表明发送 5,000 电子邮件是否可以正常工作。

在包含5,000封电子邮件的实际文件上运行此代码时,我是否有冻结或其他问题的风险?

Sub CorpCard()

Dim OutApp As Object
Dim OutMail As Object
Dim cell As Range

Application.ScreenUpdating = False
Set OutApp = CreateObject("Outlook.Application")

On Error GoTo cleanup

For Each cell In Columns("B").Cells.SpecialCells(xlCellTypeConstants)
    If cell.Value Like "?*@?*.?*" And LCase(Cells(cell.Row, "C").Value) = "yes" Then
        Set OutMail = OutApp.CreateItem(0)
        On Error Resume Next
        With OutMail
            .SentOnBehalfOfName = "urdearboy@hi.com"
            .to = cell.Value
                .Subject = "Your Employees With A Corporate Credit Card - EID - " & Cells(cell.Row, "D").Value
                .Body = "Hi " & Cells(cell.Row, "A").Value & "," _

                'Body to be patsed here

            strLocation = "C:\Users\urdearboy\Desktop\File Name " & Cells(cell.Row, "D").Value & ".xlsx"
            .Attachments.Add (strLocation)
        .Send
        End With

        On Error GoTo 0
        Set OutMail = Nothing
    End If
Next cell

cleanup:
    Set OutApp = Nothing
    Application.ScreenUpdating = True
End Sub

For Each循环放在另一个类似For i = 1 to 5000 Step 50的循环中,然后在开始实际循环之前添加Do Events来给我的计算机一些时间来追赶是个好主意吗?在继续接下来的50封电子邮件之前?我不确定这是否在Do Events的范围内。如有必要,我还可以提供计算机规格。

1 个答案:

答案 0 :(得分:2)

这对于较大的文件应该可以正常工作。通过发送这么多的电子邮件,您的运行时间很容易超过一个小时。一个好主意可能是在错误处理程序中引发一些标志,以防遇到问题。也许像这样:

    if Err then
         Msgbox "Error Encountered at Row " & cell.row
    end if 

with-block的正下方。