Microsoft Office Online
Sign in to My Office Online (What's this?) | Sign in

 
 
Help and How-to
Search
Search
 
Check for updates: (c) Microsoft
Microsoft Update
 
 
 
Warning: You are viewing this page with an unsupported Web browser. This Web site works best with Microsoft Internet Explorer 6.0 or later, Firefox 1.5, or Netscape Navigator 8.0 or later. Learn more about supported browsers.

Email this linkEmail this link Printer-Friendly VersionPrinter-Friendly Version Bookmark and ShareShare
Deleting multiple junk e-mail messages in Outlook 2002
 
Power User Corner

By Paul Cornell

Are junk e-mail messages clogging your Outlook Inbox? Wish you could do something about it? Learn about junk e-mail management features in Outlook 2002 and then modify them by using the macro code in this column.

Applies to
Microsoft Outlook® 2002

I get hundreds of e-mail messages every day. Many of these e-mail messages are unsolicited and come from senders that I don't care about; in other words, they're junk e-mail messages. Microsoft Outlook® 2002 has features that let you delete single junk e-mail messages or add sender names of single junk e-mail messages to your junk e-mail message senders list. Here's how to use these features.

To delete a single junk message from your Inbox and add the sender's name to your list of junk message sender names

  1. Right-click the junk e-mail message in your Inbox.
  2. Point to Junk E-mail.
  3. Click Add to Junk Senders list.
  4. With the junk e-mail message selected, on the Edit menu, click Delete.

To add a single junk message sender's name to your junk message senders list so that future messages from the sender go directly to your Deleted Items folder

  1. On the Tools menu, click Organize.
  2. Click Junk E-Mail.
  3. Customize the first bullet item by using the drop-down boxes, so that the sentence reads Automatically move Junk messages to Deleted Items, and then click Turn on.
  4. At the For more options label, click click here.
  5. Click Edit Junk Senders.
  6. Click Add.
  7. Type the e-mail alias of the sender to be added.
  8. Click OK.
  9. Click OK again.
  10. On the Tools menu, click Organize to return to your Inbox.

Although these features are great for single junk e-mail messages or single junk e-mail message sender names, they don't work with multiple junk e-mail messages or multiple junk e-mail message sender names. For example, if you select multiple junk e-mail messages in your Inbox and right-click them, there is no Junk E-Mail option. This is a job for a macro!

Here's how to create and run the macro in Outlook 2002

 Note   To create the macro, you must first have added at least one junk e-mail message sender name to your junk e-mail message senders list in steps 1 through 10.

  1. On the Tools menu, point to Macro, and then click Macros.
  2. In the Macro name box, type MultipleJunkEMailSenders.
  3. Click Create.
  4. Find the code that looks like this:


    Public Sub MultipleJunkEMailSenders()

    End Sub
  5. Modify the code so that it looks like this:


    ' Note: This code works only with Microsoft Outlook 2002.

    ' The list of your junk e-mail message senders is stored in a file
    ' on your hard disk named "Junk Senders.txt".

    ' Replace this string of text with the path to your
    ' Junk Senders.txt file.

    Private Const JUNK_SENDERS_FILE As String = _
    "C:\Documents and Settings\[User Name]\Application Data\Microsoft\Outlook\Junk Senders.txt"

    Public Sub MultipleJunkEMailSenders()

    Dim objExplorer As Outlook.Explorer
    Dim objMailItem As Outlook.MailItem
    Dim intItem As Integer
    Dim objFSO As Scripting.FileSystemObject
    Dim objTextStream As Scripting.TextStream

    Set objFSO = New Scripting.FileSystemObject

    If MsgBox(Prompt:="Are you sure you want to " & _
    "add all of the selected senders to your " & _
    "junk e-mail sender list and then " & _
    "delete all of the selected e-mail " & _
    "messages? Caution: This action is not " & _
    "easily reversible!", Buttons:=vbYesNo) = vbYes Then

    If objFSO.FileExists(FileSpec:=JUNK_SENDERS_FILE) = False Then
    Set objTextStream = objFSO.CreateTextFile _
    (FileName:=JUNK_SENDERS_FILE)
    Else
    Set objTextStream = objFSO.OpenTextFile _
    (FileName:=JUNK_SENDERS_FILE, IOMode:=ForAppending)
    End If

    Set objExplorer = Application.ActiveExplorer

    For Each objMailItem In objExplorer.Selection

    objTextStream.WriteLine Text:=objMailItem.SenderName

    objMailItem.Delete

    Next objMailItem

    MsgBox Prompt:="All selected senders have been added " & _
    "to your junk e-mail senders list and " & _
    "all selected e-mails have been deleted."

    End If

    End Sub
  6. Replace the string of letters C:\Documents and Settings\[User Name]\Application Data\Microsoft\Outlook\Junk Senders.txt with the path to the Junk Senders.txt file on your computer's hard disk (on the Start menu, click Search to search for the path to your Junk Senders.txt file if you don't know the path to this file).
  7. On the Tools menu, click References.
  8. In the Available References list, select the Microsoft Scripting Runtime check box.
  9. Click OK.
  10. On the File menu, click Close and Return to Microsoft Outlook.
  11. Select one or more junk e-mail messages in your Inbox.
  12. On the Tools menu, point to Macro, and then click Macros.
  13. Click MultipleJunkEMailSenders, and then click Run.
  14. Click Yes.
  15. If the message A program is trying to access e-mail addresses you have stored in Outlook. Do you want to allow this? appears, click Yes.
  16. Click OK.

The junk e-mail messages are moved to your Deleted Items folder, the sender names are added to your junk e-mail message senders list, and future e-mail messages from these senders are moved directly to your Deleted Items folder. Your junk e-mail message problems are now under control!

 Note    If you try to run the macro and see a message stating that the macros in the project are disabled, see the article Changing Macro Security Settings for details on how to fix the problem.

Keep sending that e-mail!

I look forward to receiving your e-mail messages at pwruser@microsoft.com. I really want this to be your column, so please send me your comments and favorite handcrafted Office solutions. Remember, although I do not have the time to respond to all of your e-mail, I may feature your solution in an upcoming column.


About the author

Paul Cornell works for the Office Help team. In addition to writing the Office Power User Corner column, Paul writes the Office Talk column for the Microsoft Developer Network (MSDN). He is the author of the book Accessing and Analyzing Data with Microsoft Excel.

If you like this column and want to hear about more fun and useful Office offerings, sign up for our newsletter.

Get Office 2007
Get Office 2007
advertisement