使用单元格值按地址发送唯一的电子邮件

时间:2015-05-27 13:16:04

标签: excel email excel-vba vba

我正在编写一个代码,用于向人员列表发送多封电子邮件+附件,并在Excel中提及所有详细信息。

Column A - User names
Column B - User Email
Column C - File path to be attached

我在网上发现了一个代码,并尝试更改它,但我没有让它循环遍历列中的所有项目并获取详细信息。 我也很难找到指针" .To" - "。路径"从他们正在循环的细胞中读取。

我知道这有点乱,但我完全迷失了。我想要的是通过我的所有用户循环,并根据excel表中的条目(单元格值)向每个用户发送一封带附件的电子邮件。任何帮助都将得到真正的赞赏。感谢

Dim objOutlook As Object
Dim objMail As Object
Dim rngTo As Range
Dim rngSubject As Range
Dim rngBody As Range
Dim rngAttach As Range

Set objOutlook = CreateObject("Outlook.Application")
Set objMail = objOutlook.CreateItem(0)

With ws
    Set clDest = .Range("F2")
    Set clLookup = .Range("A2")
    Set dlLookup = .Range("B2")  'email location
    Set elLookup = .Range("C2")  'file path
End With

If clLookup.Offset(1, 0) <> vbNullString Then
    rws = Range(clLookup, clLookup.End(xlDown)).Rows.Count
    Set clDest = clDest.Resize(rws, 1)
End If

With objMail
    .To = " & dlLookup.Address(False, False) & "
    .Subject = "Invoice"
    .Body = "Your Invoice for" & " " & Month & " - " & Year
    .Attachments.Add " & elLookup.Address(False, False) & "

    .Display 'Instead of .Display, you can use .Send to send the email _
                or .Save to save a copy in the drafts folder
End With

Set objOutlook = Nothing
Set objMail = Nothing
Set rngTo = Nothing
Set rngSubject = Nothing
Set rngBody = Nothing
Set rngAttach = Nothing

0 个答案:

没有答案