电子邮件VBA excel中的附件

时间:2015-10-15 09:42:40

标签: excel vba excel-vba

我正在尝试通过vcel在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

1 个答案:

答案 0 :(得分: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)以获取错误消息。