Microsoft KB Archive/253313
Article ID: 253313
Article Last Modified on 11/23/2006
- Microsoft Outlook 2000 Standard Edition
This article was previously published under Q253313
This article describes how to use Microsoft Visual Basic for Applications (VBA) to move Outlook messages to a specific folder if the messages have a blank subject.
You cannot create an Outlook rule to move messages that have a blank or null subject with the Rules Wizard. You can move messages that have a blank or null subject with a VBA code. The code in the "How to Enter the Code in the Visual Basic Editor" section of this article moves any messages that arrive in your Inbox to a subfolder that is named "Temp" (without quotation marks). After you enter the code, you can assign the Start and Stop macros to a toolbar button.
This code is designed to run from the built-in "ThisOutlookSession" code module in the Outlook 2000 Visual Basic for Applications project and assumes that you have created a new subfolder named Temp in your Inbox.
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:
How to Enter the Code in the Visual Basic Editor
To enter the code in the Visual Basic Editor:
- On the Tools menu, point to Macro, and then click Visual Basic Editor.
- In the Project pane, click to expand the folders, and then double-click the ThisOutlookSession icon.
Type or paste the following code into the Code window.
' The first two "Dim" statements declare ' global variables, and must be located in ' the "General Declarations" section at ' the beginning of the Code window. Dim WithEvents objInboxItems As Outlook.Items Dim objDestinationFolder As Outlook.MAPIFolder ' Run this code to start your rule. Sub StartRule() Dim objNameSpace As Outlook.NameSpace Dim objInboxFolder As Outlook.MAPIFolder Set objNameSpace = Application.Session Set objInboxFolder = objNameSpace.GetDefaultFolder(olFolderInbox) Set objInboxItems = objInboxFolder.Items Set objDestinationFolder = objInboxFolder.Folders("Temp") End Sub ' Run this code to stop your rule. Sub StopRule() Set objInboxItems = Nothing End Sub ' This code is the actual rule. Private Sub objInboxItems_ItemAdd(ByVal Item As Object) If Item.Subject = "" Then Item.Move objDestinationFolder End If End Sub
- On the File menu, click Save VbaProject.OTM.
- You can now run the StartRule and StopRule macros to turn the rule on and off.
- Quit the Visual Basic Editor.
After you save the macros, you can assign them to a toolbar button.
For additional information about how to assign 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
How to Customize the Macro
To have the macro run every time Outlook starts, change the name of the subroutine from StartRule to Application_Startup or call the StartRule procedure that is in the Application_Startup procedure. The Application_Startup procedure must be located in the ThisOutlookSession module. The following code is an example:
Private Sub Application_Startup() StartRule End Sub
This example moves items with blank subjects to the Temp subfolder. You can modify the value of objDestinationFolder in the code to change the destination folder for the moved items. 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 vba
Keywords: kbhowto kbprogramming KB253313