无法发送Excel单元格中指定的文件附件

时间:2014-07-18 22:34:56

标签: excel vba email excel-vba outlook-vba

我对VBA编程有些新意,所以请耐心等待。我试图通过标准电子邮件和个性化审核备忘录自动完成向组织部门负责人发送电子邮件的工作。发送给某个人的每封电子邮件也应包含相应的附件。

我有一个包含三列的电子表格:姓名,电子邮件地址,以及我想分别在A,B和C中附加的文件的位置。我已经在下面表达了最好的。

一个....................乙..................ç

名称.............电子邮件附件............

John Smith ..... a@b.com ..... Q:\ PLIU \ File1

Jane Smith ..... c@d.com .....问:\ PLIU \ File2

Jimm Smith ..... e@f.com ..... Q:\ PLIU \ File3

这是我到目前为止的代码,但我无法弄清楚如何附加写入附件列的文件,因为调试给了我"找不到这个文件。验证路径和文件名是否正确。"

Sub AttachSend()
    Dim objMail As Outlook.MailItem
    Dim intX As Integer
    Dim FileCount As Integer
    Dim MailAttachment As String
    Dim MailAddress As String

    FileCount = Application.WorksheetFunction.CountA(Range("C2:C200"))

    For intX = 1 To FileCount

        MailAttachment = Application.Cells(intX, 3).Value
        MailAddress = Application.Cells(intX, 2).Value

        Set objMail = Outlook.Application.CreateItem(olMailItem)

        objMail.Subject = "My subject line"
        objMail.Body = "My message body"
        objMail.To = MailAddress
        objMail.Attachments.Add "MailAttachment"
        objMail.Send
        Set objMail = Nothing

    Next
End Sub

提前致谢!

1 个答案:

答案 0 :(得分:3)

删除"MailAttachment"周围的引号。您想使用MailAttachment变量,但目前您正在使用字符串“MailAttachment”,当然这不是磁盘上的文件。

    MailAttachment = Application.Cells(intX, 3).Value
    MailAddress = Application.Cells(intX, 2).Value

    Set objMail = Outlook.Application.CreateItem(olMailItem)

    objMail.Subject = "My subject line"
    objMail.Body = "My message body"
    objMail.To = MailAddress
    objMail.Attachments.Add MailAttachment `Remove quotes on this line