VBA错误处理:多个附件可能不存在

时间:2018-06-28 03:32:11

标签: excel vba outlook automation

以下代码用于将每日报告附加到电子邮件并发送到预定义的分发列表。不幸的是,并非所有文件每天都存在,并且当其中一个文件丢失时,不会创建电子邮件。我试图使用“错误恢复下一个”命令无济于事。还有另一种错误处理方式吗?这两个文件都可能丢失,但通常为Report2。

Sub SendMail2()

'defined in previous sub either "" or depending on vbyesno response
'amf = "AM"
'amg = "\AM"
'amk = " AM"


Dim dlApp As Outlook.Application
Dim olMail As Outlook.MailItem

Set olApp = New Outlook.Application
Set olMail = olApp.CreateItem(olMailItem)

On Error Resume Next

With olMail
 .To = "<EMAIL LIST>"
 .CC = "<EMAIL LIST>"
 .Subject = "Subject" & " - " & Format(Date, "mm.dd.yy")
 .Body = "Attached are the report details."

'attachments may or may not exist but any of these attachments that does exist should be included in the email

 .Attachments.Add ("O:\Reports\" & Format(Date, "mm.dd.yy") & amg &     "\report " & Format(Date, "mm.dd.yy") & amk & ".pdf")
 .Attachments.Add ("O:\Reports\" & Format(Date, "mm.dd.yy") & amg & "\report " & Format(Date, "mm.dd.yy") & amk & ".xls")


'error often occurs because this report2 does not exist for the day

.Attachments.Add ("O:\Reports\" & Format(Date, "mm.dd.yy") & amg & "\report2 " & Format(Date, "mm.dd.yy") & amk & ".pdf")
.Attachments.Add ("O:\Reports\" & Format(Date, "mm.dd.yy") & amg & "\report2 " & Format(Date, "mm.dd.yy") & amk & ".xls")
 .Display
  ''olMail.Send

End With

Set olMail = Nothing
Set olApp = Nothing

Call SendMail3

End Sub

谢谢, 杰伊

0 个答案:

没有答案