如何通过退出嵌套循环来处理错误?

时间:2018-08-05 11:18:39

标签: vba outlook-vba

我想准备邮件,而有些邮件应该有多个附件。

我希望我的On Error GoTo chyba在出现错误时跳到Next i

Sub novy_mail()

    Dim attachments(1 To 5, 1 To 3) As String
        prilohy(1, 1) = "subor1.txt"
        prilohy(1, 2) = "subor2.txt"
        prilohy(2, 1) = "subor2.txt"
        prilohy(2, 2) = "subor3.txt"
        prilohy(3, 1) = "subor3.txt"
        prilohy(3, 2) = "subor4.txt"
        prilohy(4, 1) = "subor4.txt"
        prilohy(5, 1) = "subor5.txt"

    Dim oMsg As Outlook.MailItem
    Dim i As Integer
    Dim j As Integer

    For i = 1 To UBound(attachments, 1)
        Set oMsg = Application.CreateItem(olMailItem)
        With oMsg
            For j = 1 To UBound(attachments, 2)
                On Error GoTo chyba
                .Attachments.Add "C:\Users\" + attachments(i, j)
            Next j   
            .Display
        End With
chyba:
    Next i

End Sub

2 个答案:

答案 0 :(得分:1)

您可以在语句后使用On Error Resume NextError.Number的检查来跳过循环。跳过是通过设置循环计数器来使循环终止的。

不要忘记通过设置On Error Goto 0重新启用常规错误处理,否则您的程序将开始表现异常。

Sub novy_mail()
    Dim attachments(1 To 5, 1 To 3) As String
    Dim i As Integer, j As Integer

    prilohy(1, 1) = "subor1.txt"
    prilohy(1, 2) = "subor2.txt"
    prilohy(2, 1) = "subor2.txt"
    prilohy(2, 2) = "subor3.txt"
    prilohy(3, 1) = "subor3.txt"
    prilohy(3, 2) = "subor4.txt"
    prilohy(4, 1) = "subor4.txt"
    prilohy(5, 1) = "subor5.txt"

    For i = 1 To UBound(attachments, 1)
        With Application.CreateItem(olMailItem)
            For j = 1 To UBound(attachments, 2)
                On Error Resume Next
                .Attachments.Add "C:\Users\" + prilohy(i, j)
                If Err.Number <> 0 Then j = UBound(attachments, 2)
                On Error Goto 0
            Next j   
            .Display
        End With
    Next i
End Sub

请注意,oMsg变量可以多余。


一种更好用的方法是将您期望失败的代码部分提取到单独的函数中:

Sub novy_mail()
    Dim attachments(1 To 5, 1 To 3) As String
    Dim oMsg As Outlook.MailItem
    Dim i As Integer, j As Integer

    prilohy(1, 1) = "subor1.txt"
    prilohy(1, 2) = "subor2.txt"
    prilohy(2, 1) = "subor2.txt"
    prilohy(2, 2) = "subor3.txt"
    prilohy(3, 1) = "subor3.txt"
    prilohy(3, 2) = "subor4.txt"
    prilohy(4, 1) = "subor4.txt"
    prilohy(5, 1) = "subor5.txt"

    For i = 1 To UBound(attachments, 1)
        Set oMsg = Application.CreateItem(olMailItem)
        For j = 1 To UBound(attachments, 2)
            If TryAddAttachment(oMsg, "C:\Users\" + prilohy(i, j)) Then
                oMsg.Display
            Else
                j = UBound(attachments, 2)
            End If
        Next j
    Next i
End Sub

Function TryAddAttachment(mail As Outlook.MailItem, path As String) As Boolean
    On Error Resume Next
    mail.Attachments.Add path
    TryAddAttachment = (Err.Number = 0)
    ' "On Error" is automatically reset when the function ends
End Function

就个人而言,我会尽量避免在这里使用多维数组。在VBA中这不是很好。我还将创建一个仅创建单个邮件并多次调用的函数:

Sub create_mails()
    novy_mail "C:\Users\subor1.txt|C:\Users\subor2.txt"
    novy_mail "C:\Users\subor2.txt|C:\Users\subor3.txt"
    novy_mail "C:\Users\subor3.txt|C:\Users\subor4.txt"
    novy_mail "C:\Users\subor4.txt"
    novy_mail "C:\Users\subor5.txt"
End Sub

Function novy_mail(attachments As String) As Outlook.MailItem
    Dim path As Variant

    Set novy_mail = Application.CreateItem(olMailItem)

    For Each path In Split(attachments, "|")
        If Not TryAddAttachment(oMsg, path) Then Exit For
        oMail.Display
    Next
End Sub

答案 1 :(得分:0)

如果错误处理程序已经捕获到错误on error goto 0,请重用该错误处理程序。

For i = 1 To UBound(attachments, 1)
    Set oMsg = CreateItem(olMailItem)
    With oMsg
        For j = 1 To UBound(attachments, 2)
            On Error GoTo chyba
            .Attachments.Add "C:\Users\" + attachments(i, j)
        Next j   
        .Display
    End With

chyba:

    ' can be used whether an error has been caught or not
    on error goto 0

Next i

您将要评论On Error GoTo chyba并测试没有预期错误的地方。修复所有意外错误后,请取消注释。