none
Empty the Junk Emails in Outlook 2010 with a Macro RRS feed

  • Question

  • I would like to Empty the Junk Emails in Outlook 2010 with a Macro.  

    I have already went into Visual Basic and created a Module.

    In that Module, Module1, I have entered the following:

    Sub EmptyJunkEmails()
    
    End Sub

    This makes EmptyJunkEmails appear in the View Macro Box.

    My question is what code do I put in the Macro to execute the Empty the Junk Emails.

    Saturday, September 29, 2012 10:30 AM

Answers

  • You need the following

    Dim olJunkFolder As Outlook.Folder
    Dim olDeletedFolder As Outlook.Folder
    Dim olItems As Outlook.Items
    Dim olItem As Outlook.MailItem
    Dim strTime As String
    Dim strName As String
    Dim strSubject As String
    Dim olDelItem As Outlook.MailItem
    Dim i As Long, j As Long
    On Error Resume Next
    Set olJunkFolder = Session.GetDefaultFolder(olFolderJunk)
    Set olDeletedFolder = Session.GetDefaultFolder(olFolderDeletedItems)
    Set olItems = olJunkFolder.Items
    For i = olItems.Count To 1 Step -1
        Set olItem = olItems(i)
        strTime = olItem.SentOn
        strName = olItem.SenderEmailAddress
        strSubject = olItem.Subject
        olItem.Delete
        For j = olDeletedFolder.Items.Count To 1 Step -1
            Set olDelItem = olDeletedFolder.Items(j)
            If olDelItem.SentOn = strTime And _
               olDelItem.SenderEmailAddress = strName And _
               olDelItem.Subject = strSubject Then
                olDelItem.Delete
                Exit For
            End If
        Next j
    Next i
    Set olJunkFolder = Nothing
    Set olDeletedFolder = Nothing
    Set olItems = Nothing
    Set olItem = Nothing


    Graham Mayor - Word MVP
    www.gmayor.com

    • Marked as answer by Philosophiae Saturday, September 29, 2012 4:27 PM
    Saturday, September 29, 2012 1:48 PM

All replies

  • You need the following

    Dim olJunkFolder As Outlook.Folder
    Dim olDeletedFolder As Outlook.Folder
    Dim olItems As Outlook.Items
    Dim olItem As Outlook.MailItem
    Dim strTime As String
    Dim strName As String
    Dim strSubject As String
    Dim olDelItem As Outlook.MailItem
    Dim i As Long, j As Long
    On Error Resume Next
    Set olJunkFolder = Session.GetDefaultFolder(olFolderJunk)
    Set olDeletedFolder = Session.GetDefaultFolder(olFolderDeletedItems)
    Set olItems = olJunkFolder.Items
    For i = olItems.Count To 1 Step -1
        Set olItem = olItems(i)
        strTime = olItem.SentOn
        strName = olItem.SenderEmailAddress
        strSubject = olItem.Subject
        olItem.Delete
        For j = olDeletedFolder.Items.Count To 1 Step -1
            Set olDelItem = olDeletedFolder.Items(j)
            If olDelItem.SentOn = strTime And _
               olDelItem.SenderEmailAddress = strName And _
               olDelItem.Subject = strSubject Then
                olDelItem.Delete
                Exit For
            End If
        Next j
    Next i
    Set olJunkFolder = Nothing
    Set olDeletedFolder = Nothing
    Set olItems = Nothing
    Set olItem = Nothing


    Graham Mayor - Word MVP
    www.gmayor.com

    • Marked as answer by Philosophiae Saturday, September 29, 2012 4:27 PM
    Saturday, September 29, 2012 1:48 PM
  • Syntax error?

    Line:         Set olItem = olItems(i)

    Tuesday, March 28, 2017 7:23 PM