Based on the top script found at (Code will be pasted below, in case that site goes away) I want to modify it so that the objDestFolder points to a folder based on the received date of all emails, assuming the folder exists. So if I received an email Jan 1, 2017, I want to move that mail to \2017\2017-01. If I receive an email on June 1, 2017 and \2017\2017-06 does not exist, the mail is left untouched in my inbox.
What I'm planning on doing is first moving the Set objDestFolder to within the second "If" block, and set the objDestFolder to point to the folder I want, but not quite sure if this is going to refer to (Logically speaking) \Inbox\YYYY\YYYY-MM or \YYYY\YYYY-MM. If the prior, what do I need to do to get the later? How do I test to see if the folder exists prior to the move?
Would I change that line to:
??
Here is the code from the site:
-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=
NEVER send the boss to do a techs job
What I'm planning on doing is first moving the Set objDestFolder to within the second "If" block, and set the objDestFolder to point to the folder I want, but not quite sure if this is going to refer to (Logically speaking) \Inbox\YYYY\YYYY-MM or \YYYY\YYYY-MM. If the prior, what do I need to do to get the later? How do I test to see if the folder exists prior to the move?
Would I change that line to:
Code:
Set objDestFolder = objSourceFolder.Folders(Format(objVariant.SentOn,"\yyyy\yyyy-mm"))
??
Here is the code from the site:
Code:
Sub MoveAgedMail()
Dim objOutlook As Outlook.Application
Dim objNamespace As Outlook.NameSpace
Dim objSourceFolder As Outlook.MAPIFolder
Dim objDestFolder As Outlook.MAPIFolder
Dim objVariant As Variant
Dim lngMovedItems As Long
Dim intCount As Integer
Dim intDateDiff As Integer
Dim strDestFolder As String
Set objOutlook = Application
Set objNamespace = objOutlook.GetNamespace("MAPI")
Set objSourceFolder = objNamespace.GetDefaultFolder(olFolderInbox)
' use a subfolder under Inbox
Set objDestFolder = objSourceFolder.Folders("Old")
For intCount = objSourceFolder.Items.Count To 1 Step -1
Set objVariant = objSourceFolder.Items.Item(intCount)
DoEvents
If objVariant.Class = olMail Then
intDateDiff = DateDiff("d", objVariant.SentOn, Now)
' I'm using 7 days, adjust as needed.
If intDateDiff > 7 Then
objVariant.Move objDestFolder
'count the # of items moved
lngMovedItems = lngMovedItems + 1
End If
End If
Next
' Display the number of items that were moved.
MsgBox "Moved " & lngMovedItems & " messages(s)."
Set objDestFolder = Nothing
End Sub
-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=
NEVER send the boss to do a techs job