显示错误消息并恢复循环

我创build了一个VBAmacros代码来生成不同的收件人,主题,邮件内容,附件等使用各种标准的电子邮件…

代码工作正常,除了附件有问题时。 当macros未能在给定位置find相关文件时,它会popup一个消息,但不会进一步处理循环。

我的问题是,如果有人可以请看看“下一步”和“退出子”应该放在哪里,以便不停止代码循环,生成“错误popup窗口”和“电子邮件草稿”。

提前致谢…

请find下面的代码…

Sub Email_Creation_Tool() On Error GoTo ErrMsg Dim wbk As Workbook Dim OutApp As Object Dim OutMail As Object, signature As String Dim i As Range, j As Long Dim objItem As Object With ActiveSheet Set i = Range("A2", Range("A2").End(xlDown)) For j = 1 To i.Rows.Count Set OutApp = CreateObject("Outlook.Application") If Cells(j + 1, 1).Value <> "" Then Mailto = Cells(j + 1, 3).Value If Mailto = "Sentence No. 1" Then Mailto = "Friend1@abc.com" MailSubject = Cells(j + 1, 1).Value & " Sentence No. 1" MailBody = " Hi blah blah " End If If Mailto = "Sentence No. 2” Then Mailto = "Friend2@abc.com; Friend3@abc.com" CCTo = "CommonFriend@abc.com" MailSubject = Cells(j + 1, 1).Value & " Sentence No. 2" MailBody = "Hi blah blah," End If If Mailto = "Sentence No. 2” Then MailSubject = Cells(j + 1, 1).Value & " Sentence No. 3" Mailto = "Friend2@abc.com; Friend3@abc.com" CCTo = "CommonFriend@abc.com" MailBody = " Hi blah blah " End If Set OutApp = CreateObject("Outlook.Application") Set OutMail = OutApp.CreateItem(o) With OutMail .Display signature = OutMail.body With OutMail .Subject = MailSubject .To = Mailto .CC = CCTo .body = MailBody & vbNewLine & signature Name "D:\Users\MY_PC\Desktop\" & Cells(j + 1, 1).Value & ".txt" As "D:\Users\MY_PC\Desktop\" & Cells(j + 1, 1).Value & "_trial.txt" Attach = "D:\Users\MY_PC\Desktop\" & Cells(j + 1, 1).Value & "_trial.txt" .Attachments.Add (Attach) Exit Sub 'where should this be placed On Error Resume Next 'where should this be placed End With Set OutMail = Nothing Set OutApp = Nothing End With End If On Error Resume Next 'where should this be placed ErrMsg: MsgBox ("Attachment WP" & (Cells(j + 1, 1).Value) & vbNewLine & _ "Not Found/Name Incorrect") Next j End With End Sub 

我稍微编辑了你的代码,试试看:

编辑我改变的是,我用“select案件”,而不是多个“如果”,因为你有多个如果的select。 然后,我添加了“.Save”和“.Close olpromptforsave”来保存和closures消息窗口,以防有附件或没有。 转到代码跳转是很好的,就像在这种情况下一样。

所以逻辑是:

如果没有find要附加的文件,跳到错误信息,然后继续下一个J代码:保存并closures,继续到另一个“j”(nextJ代码运行,无论是否find文件)

如果find要附加的文件,请将其附加,保存,closures,跳过错误消息并继续到另一个“j”

 Sub Email_Creation_Tool() Dim wbk As Workbook Dim OutApp As Object, OutMail As Object, objItem As Object Dim i As Integer, j As Long, signature As String For j = 2 To Range("A" & Rows.Count).End(xlUp).Row If Cells(j + 1, 1).Value <> vbNullString Then Mailto = Cells(j + 1, 3).Value select case Mailto case "Sentence No. 1" Mailto = "Friend1@abc.com" MailSubject = Cells(j + 1, 1).Value & " Sentence No. 1" MailBody = " Hi blah blah " case "Sentence No. 2" Mailto = "Friend2@abc.com; Friend3@abc.com" CCTo = "CommonFriend@abc.com" MailSubject = Cells(j + 1, 1).Value & " Sentence No. 2" MailBody = "Hi blah blah," case "Sentence No. 3" MailSubject = Cells(j + 1, 1).Value & " Sentence No. 3" Mailto = "Friend2@abc.com; Friend3@abc.com" CCTo = "CommonFriend@abc.com" MailBody = " Hi blah blah " End Select Set OutApp = CreateObject("Outlook.Application") Set OutMail = OutApp.CreateItem(0) With OutMail .Display signature = OutMail.body .Subject = MailSubject .To = Mailto .CC = CCTo .body = MailBody & vbNewLine & signature Attach = "D:\Users\MY_PC\Desktop\" & Cells(j + 1, 1).Value & "_trial.txt" If Dir(Attach) = vbNullString then GoTo ErrMsg .Attachments.Add (Attach) GoTo nextJ ErrMsg: MsgBox ("Attachment WP " & (Cells(j + 1, 1).Value) & vbNewLine & "Not Found/Name Incorrect") nextJ: .Save .Close olpromptforsave End With End If Next j Set OutMail = Nothing Set OutApp = Nothing End Sub 

尝试使用Go语句请查看此链接