【发布时间】:2017-04-03 07:33:43
【问题描述】:
我正在尝试创建一个循环来查看客户列表,如果该客户有报告,请通过电子邮件向该客户发送报告。
我需要的是一个 On Error 语句,它允许跳过没有报告的客户,并允许脚本继续处理下一个客户,直到客户列表的末尾。
我目前的 On Error 语句,在所有客户循环通过后卡住,并继续在 On Error 语句中循环。
任何帮助将不胜感激!!!
sub test()
a = 2
Check:
Do Until UniqueBuyer.Range("A" & a).Value = ""
On Error GoTo ErrHandler:
Sheets(UniqueBuyer.Range("A" & a).Value).Activate
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
FolderLocation & FolderName & "\" & _
UniqueBuyer.Range("A" & a).Value & ".pdf" _
, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=Flase, OpenAfterPublish:=False
PDFFile = FolderLocation & FolderName & "\" & _
UniqueBuyer.Range("A" & a).Value & ".pdf"
Set OutLookApp = CreateObject("Outlook.Application")
Set OutlookMail = OutLookApp.createItem(0)
CombinedEmail = ""
'Clear variable - LK
On Error Resume Next
'Display email and specify To, Subject, etc
With OutlookMail
.Display
c = 4
Do Until UniqueBuyer.Cells(a, c).Value = ""
AdditionalEmail = UniqueBuyer.Cells(a, c)
CombinedEmail = CombinedEmail & ";" & AdditionalEmail
.to = CombinedEmail
c = c + 1
Loop
.cc = ""
.BCC = ""
.Subject = "Weekly Wooltrade Summary " & Left(Master.Range("X2"), 3)
.Body = ""
.Attachments.Add PDFFile
'.Send
End With
On Error GoTo 0
a = a + 1
Loop
Exit Sub
ErrHandler:
a = a + 1
GoTo Check
End Sub
【问题讨论】:
-
您不能使用
GoTo退出错误处理程序。使用Resume Check而不是GoTo Check。Check标签可能应该在循环内部,而不是外部。也许就在a = a+1行之前。 -
感谢@VincentG!将 Check Label 放在 a=a+1 行之后和 Loop 行之前会更好吗?例如,a 不会从 a=2 跳转到 a=4?
-
把它放在 a=a+1 之前并从处理程序中删除同一行是最好的选择,恕我直言,但我没有深入了解您的代码。
-
谢谢@VincentG 那太好了!