1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| Sub Test() Dim Mail As Outlook.Application Set Mail = New Outlook.Application Dim objMail As Outlook.MailItem Set objMail = Mail.CreateItem(olMailItem) Dim oRng As Range Dim oCell As Range Dim dtToday As Date Dim dtDiff As Integer Dim mtCells As New Collection dtToday = Date Set oRng = Worksheets("Sheet1").Range("A2", "A6") For Each oCell In oRng dtDiff = DateDiff("d", dtToday, oCell.Value) If dtDiff <= 3 And dtDiff >= 0 Then Dim rng As Range Set rng = Cells(Right(oCell.Address, 1), "B") sMsg = "物料 " & rng.Value & " " & "过期时间 " & oCell.Value & " " & "还有 " & dtDiff & "天!" mtCells.Add (sMsg) End If Next Dim result As String Dim i As Long For i = 1 To mtCells.Count result = result & mtCells(i) & vbCrLf & vbCrLf Next i With objMail .Subject = "My Test Mail" .To = "1402271195@qq.com" .Body = result .Display End With End Sub
|