Restrict()
does not seem to accept a date value when it is specified outside.
Public Sub EBS()
Dim oMail As MailItem
Dim sPath As String
Dim dtDate As Date
Dim dtRecDate As Date
Dim sName As String
Dim oNameSpace As Outlook.NameSpace
Dim oInboxFolder As Outlook.Folder
Dim oSentFolder As Outlook.Folder
Dim i As Long
Set oNameSpace = Application.GetNamespace("MAPI")
Set oInboxFolder = oNameSpace.GetDefaultFolder(olFolderInbox)
Set oSentFolder = oNameSpace.GetDefaultFolder(olFolderSentMail)
dtRecDate = DateAdd("d", -180, Now)
Set setItems = oInboxFolder.Items
Set RestrictedItems = setItems.Restrict("[ReceivedTime] < dtRecDate AND [MessageClass] = 'IPM.Note'")
For i = RestrictedItems.Count To 1 Step -1
Set oMail = RestrictedItems.item(i)
sName = oMail.Subject
dtDate = oMail.ReceivedTime
sName = Format(dtDate, "yyyymmdd", vbUseSystemDayOfWeek, vbUseSystem) & Format(dtDate, "_hhnnss", vbUseSystemDayOfWeek, vbUseSystem) & "_" & sName & ".msg"
sName = Left(sName, 256)
sPath = "C:\ARCHIVE\OUTLOOK\Inbox\"
Debug.Print dtRecDate
oMail.SaveAs sPath & sName, olMSG
oMail.Delete
Next i
End Sub
The restriction works when, for example, '2014/06/13' is used instead of dtRecDate
.
When dtRecDate
is used, it does not restrict any item.
Can you please help?
I see the following filter criteria in the code:
"[ReceivedTime] < dtRecDate AND [MessageClass] = 'IPM.Note'"
You can't declare object in the string. It will not be converted to string automatically. You have to do so in the code, for example:
"[ReceivedTime] < '" + Format(Date, "yyyy/mm/dd") +"' AND [MessageClass] = 'IPM.Note'"
The Restrict method has the following sample on the page in MSDN:
sFilter = "[LastModificationTime] > '" & Format("1/15/99 3:30pm", "ddddd h:nn AMPM") & "'"