2015-10-15 191 views
1

我想通過在Excel中的VBA發送電子郵件,所有的作品罰款電子郵件附件。它似乎沒有鏈接它。問題在哪裏? 字符串附加指向正確的文件。電子郵件附件VBA excel

Dim OutApp As Object 
Dim OutMail As Object 
Dim email 
Dim attach 

email = writeEmailAddress() 
attach = attachement() 

Sheets("Mail").Range("B1") = email 
Sheets("Mail").Range("B2") = "xxxxxx" 
Sheets("Mail").Range("B3") = "xxxxxxx" 
Sheets("Mail").Range("B4") = attach 
MsgBox attach 

Set OutApp = CreateObject("Outlook.Application") 
Set OutMail = OutApp.CreateItem(0) 

On Error Resume Next 
With OutMail 
    .SendKeys "^{ENTER}" 
    .to = "xxxxx" 
    .CC = "" 
    .BCC = "" 
    .Subject = Sheets("Mail").Range("B5").Value 
    .Body = Sheets("Mail").Range("B6").Value 
    'You can add other files also like this 
    .Attachments.Add attach ' <--------------------------------This is causing troubble 
    .Display 
End With 
On Error GoTo 0 

Set OutMail = Nothing 
Set OutApp = Nothing 

回答

2

變化,

.Attachments.Add attach 

...來,

If CBool(Len(Dir(attach, vbNormal))) Then 
    .Attachments.Add attach, 1 '<~~ 1 is olByValue 
Else 
    Debug.Print "Cannot find '" & attach & "'" 
End If 

如果附件沒有添加到您的電子郵件項目,檢查VBE的立即窗口(如按Ctrl + G)爲錯誤消息。

+0

好的發現你的問題是正確的路徑文件是錯誤的!非常感謝你 –