Excel向一列中的所有人发送电子邮件,只需要通过电子邮件发送特定单元格中的人员。如何?

时间:2016-01-08 21:34:09

标签: excel vba email

我正在创建一个电子表格,该报告将在报告到期前30,15和7天向主管发送电子邮件。一切正常,除了它向L列中的每个人发送电子邮件,而不是L列中的指定人员。

enter image description here

请帮忙。我复制并粘贴了下面的代码。

Public Sub GetDates()
    Dim rw As Integer
    Dim subj As String
    rw = 2

    With ActiveSheet

       Do Until .Range("A" & rw) = ""
            If .Range("M" & rw) = "" Then
                If DateAdd("D", 30, Date) = .Range("G" & rw) Then
                    Call SendEmail(Range("A" & rw), Range("B" & rw), 30, Range("L" & rw), False)
                ElseIf DateAdd("D", 15, Date) = .Range("G" & rw) Then
                    Call SendEmail(Range("A" & rw), Range("B" & rw), 15, Range("L" & rw), False)
                ElseIf DateAdd("D", 7, Date) = .Range("G" & rw) Then
                    Call SendEmail(Range("A" & rw), Range("B" & rw), 7, Range("L" & rw), False)
                End If
            End If

            If Day(Date) = 1 And .Range("G" & rw) < Date And .Range("M" & rw) = "" Then
                subj = subj & .Range("A" & rw) & ", " & .Range("B" & rw) & "--" & .Range("C" & rw) & " Report Past Due" & vbCrLf
            End If
            rw = rw + 1

        Loop

        If subj <> "" Then
            Call SendEmail(subj, "", 0, "supervisor@company.com", True)
            Call SendEmail(subj, "", 0, "aothersupervisor@company.com", True)
        End If
    End With

End Sub

Public Sub SendEmail(lName As String, fName As String, nDays As Integer, sTo As String, LastEmail As Boolean)

    Dim iMsg As Object
    Dim iConf As Object
    Dim cell As Range
    Dim Flds As Variant



    Set iMsg = CreateObject("CDO.Message")
    Set iConf = CreateObject("CDO.Configuration")

     iConf.Load -1    ' CDO Source Defaults
        Set Flds = iConf.Fields
        With Flds
            .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
            .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") _
                           = "server"
            .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
            .Update
        End With

For Each cell In ActiveSheet.Columns("L").Cells
        If cell.Value Like "?*@?*.?*" Then
            Set iMsg = CreateObject("CDO.Message")
    With iMsg
        Set .Configuration = iConf
        .to = cell.Value
        .CC = ""
        .BCC = ""
        .From = """Report Due"" <donotreply@company.com>"
        .Subject = "Report Due"
        .HTMLBody = lName & ", " & fName & "  <a href='http://www.website.com'>Probation Report</a> / <a href='http://www.website.com'>IDP Report</a> Due in " & nDays & " days"
        .Send
            End With
            Set iMsg = Nothing
    End If
Next cell

With Application
    .EnableEvents = True
    .ScreenUpdating = True
End With

End Sub

1 个答案:

答案 0 :(得分:0)

SendEmail子例程中,您从不使用Column L电子邮件参数sTo,而是例程迭代L列中的每个单元格:

For Each cell In ActiveSheet.Columns("L").Cells
     ...
Next cell

删除此For循环并使用sTo

If sTo Like "?*@?*.?*" Then
    Set iMsg = CreateObject("CDO.Message")
    With iMsg
        Set .Configuration = iConf
        .to = sTo
        .CC = ""
        .BCC = ""
        .From = """Report Due"" <donotreply@company.com>"
        .Subject = "Report Due"
        .HTMLBody = lName & ", " & fName & "  <a href='http://www.website.com'>Probation Report</a> / <a href='http://www.website.com'>IDP Report</a> Due in " & nDays & " days"
        .Send
    End With

    Set iMsg = Nothing
End If

顺便说一句,在您的GetDates子例程中,考虑使用Select Case而不是重复的ElseIf,因为您有多种管理条件:

Select Case .Range("G" & rw)

    Case DateAdd("D", 30, Date)
        Call SendEmail(Range("A" & rw), Range("B" & rw), 30, Range("L" & rw), False)

    Case DateAdd("D", 15, Date)
        Call SendEmail(Range("A" & rw), Range("B" & rw), 15, Range("L" & rw), False)

    Case DateAdd("D", 7, Date)
        Call SendEmail(Range("A" & rw), Range("B" & rw), 7, Range("L" & rw), False)

End Select

或者,甚至更短:

Select Case .Range("G" & rw) - Date

    Case 7, 15, 30
        Call SendEmail(Range("A" & rw), Range("B" & rw), _
                       .Range("G" & rw) - Date, Range("L" & rw), False)

End Select