VBA to permanently delete emails instead of moving them to the deleted folder

I am using the following code in my Application_NewMail event to pre-screen my emails and get rid of some of the SPAM coming in. Unfortunately it just moves these emails to the deleted folder and I was wondering if there was some way of permanently deleting instead of putting them in the deleted folder. Any suggestions.

Public Sub JunkMail()
  ' Declare objects
  Dim olNameSpace As Outlook.NameSpace
  Dim olInbox As Outlook.MAPIFolder
  Dim olAttach As Outlook.Attachment
  Dim olItem As Object
 
  Set olNameSpace = Application.GetNamespace("MAPI")
 
  ' Get the default inbox
  Set olInbox = olNameSpace.GetDefaultFolder(olFolderInbox)
 
  ' Loop on the items collection from the inbox folder
  For Each olItem In olInbox.Items
    ' Check if the item is a mail (could be a task, etc.)
    If olItem.Class = olMail Then
        ' Check the sender name and unread status
        If olItem.UnRead Then
         If Nz(InStr(1, olItem.SentOnBehalfOfName, "Rolex")) > 0 Then
            olItem.Delete
         ElseIf Nz(InStr(1, olItem.SentOnBehalfOfName, "Louis-Vuitton")) > 0 Then
            olItem.Delete
         ElseIf Nz(InStr(1, olItem.SentOnBehalfOfName, "Prada")) > 0 Then
            olItem.Delete
         ElseIf Nz(InStr(1, olItem.SentOnBehalfOfName, "Penis")) > 0 Then
            olItem.Delete
         ElseIf Nz(InStr(1, olItem.SentOnBehalfOfName, "Premier Players Club")) > 0 Then
            olItem.Delete
         ElseIf Nz(InStr(1, olItem.Subject, "VIGARA")) > 0 Then
            olItem.Delete
         ElseIf Nz(InStr(1, olItem.SentOnBehalfOfName, "Vegas")) > 0 Then
            If Nz(InStr(1, olItem.Body, "Virtual VegasClub")) > 0 Then
               olItem.Delete
            End If
         End If
        End If
    End If
  Next
 
  Set olItem = Nothing
  Set olInbox = Nothing
  Set olNameSpace = Nothing
   
End Sub
Rob4077Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris BottomleySoftware Quality Lead EngineerCommented:
Add the function KillItem below and pass it the item you wish to delete ... it deletes it to the deleted items folder then deletes it again ... in one pass as far as your code is concerned.

Chris
Function killItem(itm As Object) As Boolean
Dim strID As String
'killitem application.ActiveExplorer.Selection(1)

    strID = itm.EntryID
    itm.Delete
    Application.Session.GetItemFromID(strID).Delete

End Function

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Chris BottomleySoftware Quality Lead EngineerCommented:
In your syntax that would be:

killitem olItem

Chris
0
Rob4077Author Commented:
Fantastic. Thank you!!!!
0
Chris BottomleySoftware Quality Lead EngineerCommented:
It's a shame when we miss questions like this as it was easy enough for any of us ... Just slipped under everyone's radar I guess.

Glad to have helped though.

Chris
0
Rob4077Author Commented:
Hi Chris,  I don't know why you think it slipped under the radar. I got my reply in a time frame I am happy with. Thanks again
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Outlook

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.