excel 如何将日期范围条件添加到电子邮件导入?

oknrviil  于 2023-05-08  发布在  其他
关注(0)|答案(2)|浏览(157)

我有一些编码,导入几个文件夹的存储电子邮件到Excel的价值。编码从文件夹中带回所有存储的电子邮件。
我只想要上个月的电子邮件。我有公式到位,这是动态的,并自动更新前几个月的开始和结束日期。我知道我需要在编码中引用这些范围。
我定义了范围Range("BOMD")Range("EOMD")-这些是公式驱动的单元格,分别计算每月的第一天和最后一天。
如何将日期条件添加到下面的编码中?

Sub test()

    Dim olApp As Outlook.Application
    Dim olNS As Outlook.Namespace
    Dim olFldr As Outlook.MAPIFolder
    Dim olItem As Object
    Dim olMailItem As Outlook.MailItem
    Dim ws As Worksheet
    Dim iRow As Long
    Dim hdr As Variant
    Dim iFldr As Long
    
    Set ws = ThisWorkbook.Worksheets("EmailImport")
    
    Set olApp = New Outlook.Application
    Set olNS = olApp.GetNamespace("MAPI")
     
    With ws
        iRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With
    
    Application.ScreenUpdating = False
    
    For iFldr = 1 To 18
        Select Case iFldr
            Case 1
                Set olFldr = olNS.Folders("test1").Folders("test1").Folders("test1")
                Set olFldr = olFldr.Folders("test1a")
            Case 2
                Set olFldr = olNS.Folders("test1").Folders("test1").Folders("test1")
                Set olFldr = olFldr.Folders("test1b")
                
            Case Else
        End Select

        For Each olItem In olFldr.Items
            If olItem.Class = olMail Then
                Set olMailItem = olItem
                With olMailItem
                    iRow = iRow + 1
                    If Not .Sender Is Nothing Then ws.Cells(iRow, "A") = .Sender
                    ws.Cells(iRow, "B") = .SenderEmailAddress
                    ws.Cells(iRow, "C") = .SenderName
                    ws.Cells(iRow, "D") = .Subject
                    ws.Cells(iRow, "E") = .ReceivedTime
                    ws.Cells(iRow, "F") = .Categories
                    ws.Cells(iRow, "G") = .TaskCompletedDate
                    ws.Cells(iRow, "H") = olFldr.Name
                End With
            End If
        Next olItem
    Next iFldr

    With ws
        hdr = Array("Sender", "SenderEmailAddress", "SenderName", "Subject", "ReceivedTime", "Categories", "TaskCompletedDate", "Folder")
        .Range("A1").Resize(, UBound(hdr)) = hdr
        .Columns.AutoFit
    End With

End Sub
kzmpq1sx

kzmpq1sx1#

您可以计算发送时间和处理时间之间的差异:

Dim Difference As Long    
Difference = DateDiff("d", olItem.SentOn, Now)
If Difference < 30 Then
    ' Do Stuff
End If

如果您想要进行日期比较,这应该可以通过一个equality语句来实现,但要确保日期事先转换为CDate值

vlurs2pr

vlurs2pr2#

您可以在ReceivedTime属性上创建一个限制,并将其传递给Items.Restrict(这将返回一个新的受限制的Items集合:

([ReceivedTime] > '04/01/2021') AND ([ReceivedTime] < '04/21/2021')

相关问题