我正在创建宏以将主题和收到日期的电子邮件发送到Excel工作表。 我使用Restrict方法获取与过滤器匹配的新集合。我的过滤器是指定日期。 e,g)可能是所有电子邮件的1/1/2018至1/31/2018。
DateStr,DateEnd var由用户在Excel中选择。我将值设置为 DateStr = 1/14/2018和DateEnd = 1/16/2018。当我运行此代码时,它显示并获取文件,如1/16/2018文件3 EA(应该是4),1/15/2018文件4(没关系),1/14/201文件1(应该是4) )
Sub GetFromOutlook()
Dim OutlookApp As Outlook.Application
Dim OutlookNamespace As Namespace
Dim Folder As MAPIFolder
Dim OutlookMail As Variant
Dim i As Integer
Dim olItems As Outlook.Items
Dim myItems As Outlook.Items
Dim DateStr As Date
Dim DateEnd As Date
Dim oOlResults As Object
Dim sFilter As String
Set OutlookApp = New Outlook.Application
Set OutlookNamespace = OutlookApp.GetNamespace("MAPI")
Dim olShareName As Outlook.Recipient
Set olShareName = OutlookNamespace.CreateRecipient("Mailbox.teamsharemail@example.ca")
Set Folder = OutlookNamespace.GetSharedDefaultFolder(olShareName, olFolderInbox).Folders("subfolder1").Folders("subfolder2")
Set olItems = Folder.Items
DateStr = Format(Range("From_Date").Value, "mm/dd/yyyy")
DateEnd = Format(Range("To_Date").Value, "mm/dd/yyyy")
DateStr = Range("From_Date").Value
DateEnd = Range("To_Date").Value
Set myItems = olItems.Restrict("(([ReceivedTime] > '" & DateStr & "') AND ([ReceivedTime] < '" & DateEnd & "')) AND ([SenderName] = ""no-reply@croesus.com"")")
i = 1
For Each myitem In myItems
' MsgBox myitem.ReceivedTime
Range("eMail_subject").Offset(i, 0).Value = myitem.Subject
Range("eMail_date").Offset(i, 0).Value = myitem.ReceivedTime
i = i + 1
Next myitem
Set Folder = Nothing
Set OutlookNamespace = Nothing
Set OutlookApp = Nothing
End Sub