VBA在输入的单元格日期更改时发送电子邮件

时间:2014-11-20 23:29:37

标签: vba outlook outlook-vba

我遇到了这个编码的问题。我正在尝试对其进行编码,以便在B列的某张纸上输入日期时自动发送电子邮件。

基本上,将要发生的是员工每天都会获得新的任务。日期将根据输入的日期而变化。

如果代码能识别输入的当前日期并将电子邮件发送给特定员工,我会很高兴。这是我的示例代码,但它只设置为数值,我不知道如何更改它。请帮忙!

此代码位于特定工作表中。

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Cells.count > 1 Then Exit Sub
    If Not Application.Intersect(Range("A:A"), Target) Is Nothing Then
        If IsNumeric(Target.Value) And Target.Value >= 1 Then
            Call Send_Email
        End If
    End If
End Sub

此代码进入模块

   Sub Send_Email()
        Dim OutApp As Object
        Dim OutMail As Object
        Dim strbody As String

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

    strbody = "Hi All" & vbNewLine & vbNewLine & _
              "You may have new cases." & vbNewLine & _
              "Please review and disposition them." & vbNewLine & _
              "Thank you"

    On Error Resume Next
    With OutMail
        .To = "Calheers Unit"
        .CC = ""
        .BCC = ""
        .Subject = "New Case Assignments"
        .Body = strbody
        .Send
    End With
    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub

1 个答案:

答案 0 :(得分:0)

如果您想检查输入的日期是否为今天,只需检查 target.value 是否等于 Date 日期代表今天在VBA中的日期,例如excel中的 = Today()

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Cells.Count > 1 Then Exit Sub
    If Target.Column = 2 Then               'If target is in column B
        If Target.Value = Date Then         'If the target value is today
           Call Send_Email
        End If
    End If
End Sub