循环访问单元格以发送电子邮件

时间:2013-11-01 18:56:31

标签: excel excel-vba excel-2007 vba

您好我有一个宏应该发送电子邮件到我的C列上的地址,并复制到F列中的电子邮件地址和一些文本......但我的问题是它没有循环遍历每个名​​称和只是去了最后一个......有人可以帮我弄清楚我做错了吗?

Sub Send_Email()

Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Dim cel As Range

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

For Each cel In Range(("C2"), Range("C2").End(xlDown))

    strbody = "Hi there" & vbNewLine & vbNewLine & _
            "My name Is William" & vbNewLine & _
            "I work at Fair" & vbNewLine & _
            "Bye" & vbNewLine & _
            "WH"

    On Error Resume Next
    With OutMail
        .To = cel.Value    '"email.address@fair.com"
        .CC = cel.Offset(0, 3).Value & "Ivan.Martinez@fair.com"
        .BCC = ""
        .Subject = "Information You Requested"
        .Body = strbody
        .Display
        '.Attachments.Add ("C:\test.txt")
        '.Send
    End With
    On Error GoTo 0
Next cel

Set OutMail = Nothing
Set OutApp = Nothing

End Sub

2 个答案:

答案 0 :(得分:0)

尝试将此添加到您的代码中:

Dim lastRow As Long  
Dim Rng As Range  

lastRow = Range("C:C").Find("*", Range("C1"), Searchdirection:=xlPrevious).Row
Set Rng = Range(Cells(2,"C"),Cells(lastRow,"C"))

For Each Cel in Rng

'The rest of your code goes here.

这是设置范围的一种更强大的方式。

答案 1 :(得分:0)

For Each cel In Range(("C2"), Range("C2").End(xlDown)).Cells