2017-03-24 124 views
0

我有一個自動發送或顯示電子郵件的代碼。顯示電子郵件而不發送

Set OutApp = CreateObject("Outlook.Application") 
    Set outmail = OutApp.CreateItem(olMailItem) 

    With outmail 
     .To = c.Value 
     .BCC = "[email protected]" 
     .Subject = Range("B1") 
     .HTMLBody = RangetoHTML(s) 
    On Error Resume Next 
     .Attachments.Add (DFile1) 
     .Attachments.Add (DFile2) 
     .Attachments.Add (DFile3) 
     .Attachments.Add (DFile4) 
     .Attachments.Add (DFile5) 
     '.Send  'to send directly 
     .Display 'to display email 
    End With 

當我使用.Send它完美的作品,但是當我使用.Display宏觀運行正常,但沒有得到顯示

+0

爲什麼有'在錯誤恢復下一個'? – 0m3r

+0

以防萬一其中一個附件不可用 – sharkantipav

+0

刪除它並運行測試以查看它是否有效。關於文件不可用我敢肯定還有其他方法來檢查 – 0m3r

回答

2

我想你想要做的是檢查每個附件,然後跳過使用On Error Resume Next所以你能做到這一點,我不喜歡它..

On Error Resume Next 
.Attachments.Add (DFile1) 
On Error Resume Next 
.Attachments.Add (DFile2) 
On Error Resume Next 
.Attachments.Add (DFile3) 
On Error Resume Next 
.Attachments.Add (DFile4) 
On Error Resume Next 
.Attachments.Add (DFile5) 
On Error GoTo 0 

但我會做的就是這樣的事情

 '// Attachment Path 
    AtmtsPath = "C:\Temp\" 

    '// Add attachments to the message. 
    Atmts = Dir(AtmtsPath & "*.*") 

    Do While Len(Atmts) > 0 
     .Attachments.Add AtmtsPath & Atmts 
     Atmts = Dir 
    Loop 
相關問題