Cyberiafreak

"Fortunate are those who take the first steps.” ― Paulo Coelho

Outlook:VBA#1 [How to move older mails to archive folders using code]

Private Sub Application_Startup()

Dim objOutlook As Outlook.Application
Dim objInboxFolder As Outlook.MAPIFolder
Dim myNewFolder As Outlook.Folder
Dim objNamespace As Outlook.NameSpace
Set objOutlook = Application
Set objNamespace = objOutlook.GetNamespace("MAPI")
Set objInboxFolder = objNamespace.GetDefaultFolder(olFolderInbox)
Set newFolder = objInboxFolder.Folders.Add("Test-Archived-Jan2014")
Dim movedItemCount As Long

For intCount = objInboxFolder.Items.Count To 1 Step -1
Set objVariant = objInboxFolder.Items.Item(intCount)
DoEvents
If objVariant.Class = olMail Then
intDateDiff = DateDiff("d", objVariant.SentOn, Now)
If intDateDiff > 10 Then
' Set objArchiveFolder = objNamespace.Folders("maheshk@keepmovin.com"). _
' Folders("Inbox").Folders("Old")
objVariant.Move newFolder
movedItemCount = movedItemCount + 1
Set objArchiveFolder = Nothing
End If
End If
Next
MsgBox "Done, archived:#" & movedItemCount

End Sub

Advertisements

January 31, 2014 - Posted by | Outlook, VBA

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: