有一個簡單的代碼來發送帶有目錄附件的電子郵件。
問題是當其中一個檔案丟失時,不會發送電子郵件。即使只有一個附件,我也想發送。
Sub Send_email_IPS()
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
.to = "[email protected]"
.CC = ""
.BCC = ""
.Subject = "Update " & Date & " " & Time
.HTMLbody = "Hello " & "<br>" & "<br>" & "Please find attached latest update" & "<br>" & "<br>" & "Best Regards" & "<br>" & "<br>" & "Me"
.Attachments.Add "C:\Users\testuser\Work Folders\Desktop\KB4 Reporting Macro\IPS.xlsx"
.Attachments.Add "C:\Users\testuser\Work Folders\Desktop\KB4 Reporting Macro\IPS (St Helens).xlsx"
.Display
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
uj5u.com熱心網友回復:
如評論中所述,僅當檔案存在時才添加附件。
Const File1 As String = "C:\Users\testuser\Work Folders\Desktop\..."
Const File2 As String = "C:\Users\testuser\Work Folders\Desktop\..."
With OutMail
'...
If Len(Dir(File1)) > 0 Then .Attachments.Add File1
If Len(Dir(File2)) > 0 Then .Attachments.Add File2
.Display
End With
要在未找到檔案時做筆記,請使用Else
條件寫入電子郵件正文。更改內容以滿足您的需求。
If Len(Dir(File1)) > 0 Then .Attachments.Add File1 Else .HTMLbody = .HTMLbody & "<p> File not found: '" & File1 & "'</p>"
If Len(Dir(File2)) > 0 Then .Attachments.Add File2 Else .HTMLbody = .HTMLbody & "<p> File not found: '" & File2 & "'</p>"
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/527813.html
標籤:vba
上一篇:C#過濾元組和洗掉演員表