Microsoft KB Archive/249597
Article ID: 249597
Article Last Modified on 11/23/2006
- Microsoft Outlook 2000 Standard Edition
This article was previously published under Q249597
This article describes how to use Microsoft Visual Basic for Applications to move Outlook mail messages to a specific folder if the messages are older than a certain number of days.
The Rules Wizard does not provide the ability to check the age of mail in your Inbox and then apply a rule to the mail. You can, however, create a macro that either runs every time you start Outlook, or when you click a toolbar button.
When you run the sample macro, it prompts you to select a folder to move the items to. Mail messages older than six days will be moved.
Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Partner or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please visit the following Microsoft Web site:
For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:
To Create the Macro
- On the Tools menu, point to Macro, and then click Visual Basic Editor.
- In the Project window, expand the folders, and then double-click the ThisOutlookSession icon.
Type the following code into the code window.
Sub MoveOldItems() Dim olns As Outlook.NameSpace Dim oConItems As Outlook.Items Dim iNumItems As Integer Dim dDate As Date Const Days = 6 Set objNS = Application.GetNamespace("MAPI") Set oInboxItems = objNS.GetDefaultFolder(olFolderInbox).Items Set objTargetFolder = Outlook.Session.PickFolder iNumItems = oInboxItems.Count For I = iNumItems To 1 Step -1 Set objCurItem = oInboxItems.Item(I) If TypeName(objCurItem)= "MailItem" Then ' Move only mail messages dDate = objCurItem.ReceivedTime If DateDiff("d", dDate, Now) > Days Then objCurItem.Move objTargetFolder End If End If Next MsgBox "Finished moving items." Set objInboxItems = Nothing Set objTargetFolder = Nothing Set objNS = Nothing End Sub
- On the File menu, click Save VbaProject.OTM.
- Quit the Visual Basic Editor.
Once you save the macro, you can assign it to a toolbar button.
For additional information about assigning a macro to a toolbar button, click the article number below to view the article in the Microsoft Knowledge Base:
252426 OL2000: How to Assign a Macro to a Toolbar Button
When you run the macro the mouse pointer will not change to an hourglass; however, when all the items are checked and moved, the macro displays a message indicating that it is finished.
To Customize the Macro
- To have the macro run every time Outlook starts, change the name of the subroutine from MoveOldItems to Application_Startup.
To change the number of days before an item is moved, change the number on the following line of code:
Const Days = 6
- The code uses the PickFolder method to display a window that lets you specify which folder the items should be moved to. You can change this line of code so that it will always move the items to a specific folder. There are various ways to refer to a folder programmatically.For additional information about how to refer to a folder using the Outlook object model, click the article number below to view the article in the Microsoft Knowledge Base:
208520 OL2000: Programming Examples for Referencing Items and Folders
For additional information about available resources and answersto commonly asked questions about Outlook solutions, click the article number below to view the article in the Microsoft Knowledge Base:
146636 OL2000: Questions About Custom Forms and Outlook Solutions
Additional query words: OutSol OutSol2000 OL2K
Keywords: kbhowto kbprogramming KB249597