如果单元格中的日期比当前日期早2年,则发送邮件

时间:2017-03-30 10:44:24

标签: excel vba excel-vba

我创建了一个宏,列出了excel工作表中所有文件和子文件夹的名称,路径,创建日期,修改日期,这个宏工作正常。

但是现在我想创建一个宏,它可以检查整个文件和文件夹列表,如果任何列出的文件/文件夹的日期创建日期(列F)大于当前日期的2年,它应该发送一个自动邮件,提供文件的名称,路径以及创建和修改信息(基本上将该特定行的所有信息复制)给我。

请在下面找到我收到错误的代码(运行时错误) 这一行:

Sub send_files()
Dim OutApp As Object
Dim OutMail As Object
Dim sh As Worksheet
Dim cell As Range
Dim FileCell As Range
Dim rng As Range
Dim sDate As String

With Application
    .EnableEvents = False
    .ScreenUpdating = False
End With

Set sh = ThisWorkbook.Sheets("Sheet1")
Set OutApp = CreateObject("Outlook.Application")
sDate = Date

For Each cell In sh.Columns("F").Cells
    Days = DateDiff("yyyy", Cells.Value, sDate)
    If Days > 2 Then
                Set OutMail = OutApp.CreateItem(0)

        With OutMail
            .to = cell.Value
            .Subject = "Testfile"
            .Body = "Hi " & cell.Offset(0, -1).Value
            .send

        End With
    End If
Next cell

Set OutApp = Nothing
With Application
    .EnableEvents = True
    .ScreenUpdating = True
End With
End Sub

错误发生在:

Days = DateDiff("yyyy", Cells.Value, sDate)

任何人都可以帮助解决此错误吗?

1 个答案:

答案 0 :(得分:4)

尝试更改

Days = DateDiff("yyyy", Cells.Value, sDate)

Days = DateDiff("yyyy", cell.Value, sDate)