我有一些编码,导入几个文件夹的存储电子邮件到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
2条答案
按热度按时间kzmpq1sx1#
您可以计算发送时间和处理时间之间的差异:
如果您想要进行日期比较,这应该可以通过一个equality语句来实现,但要确保日期事先转换为CDate值
vlurs2pr2#
您可以在
ReceivedTime
属性上创建一个限制,并将其传递给Items.Restrict
(这将返回一个新的受限制的Items
集合: