PDA

View Full Version : [SOLVED:] Deleting an email using VBA in outlook when the subject name contains certain words



jperki39
10-19-2013, 09:17 AM
I'm new to VBA in outlook, but I've used VBA in excel for a while.

At work I use microsoft outlook, and I've run out of space for outlook "rules".

I'm trying to create a VBA script that will check my email as I get it, and if there is a email with a specified string in the subject it will delete it.

This is what I tried to code but I couldn't get it to work:

Public Sub process_email(itm As Outlook.MailItem)
Dim new_msg As MailItem
If new_msg.Subject Like "*keyword*" Then
new_msg.Delete
End If
End Sub

Can anyone help?

mrojas
10-19-2013, 01:21 PM
You most likely would have to do some sort of substring (look up split function) comparison on the subject string. "Like" is ok to use in a query, but not on if statement.

jperki39
10-19-2013, 01:25 PM
You most likely would have to do some sort of substring (look up split function) comparison on the subject string. "Like" is ok to use in a query, but not on if statement.

I will need to be able to use wildcards, I've used "like" before with excel vba - can you post some example code?

for example I want to set it to delete an email if it contains the word "yellow" in the subject

Subject: This is the yellow submarine (will delete)
Subject: This is a yellow banana (will delete)

mrojas
10-19-2013, 01:45 PM
Look up the Split function. This function literally splits a substring. It returns a zero-based array with as many elements as there aer words in the subject. Then you simply have to traverse the array, and if any of the array's elements match your criteria, zap kabum, finito, delete message.

jperki39
10-19-2013, 01:58 PM
Look up the Split function. This function literally splits a substring. It returns a zero-based array with as many elements as there aer words in the subject. Then you simply have to traverse the array, and if any of the array's elements match your criteria, zap kabum, finito, delete message.

Let me start with something simpler.

Lets say I want to delete an email with a given subject - (not just part of the subject)

How would I even do that? I've been searching, like I said I am not familair with VBA for outlook, only excel.

skatonni
10-19-2013, 02:00 PM
Public Sub process_email(itm As Outlook.MailItem)
Dim new_msg As MailItem
If new_msg.Subject Like "*keyword*" Then
new_msg.Delete
End If
End Sub


for example I want to set it to delete an email if it contains the word "yellow" in the subject

Subject: This is the yellow submarine (will delete)
Subject: This is a yellow banana (will delete)


You are passing itm there is no new_msg


Public Sub process_email(itm As Outlook.MailItem)
If InStr(itm.Subject , "yellow") > 0 Then
itm.Delete
End If
End Sub

jperki39
10-19-2013, 02:09 PM
You are passing itm there is no new_msg


Public Sub process_email(itm As Outlook.MailItem)
If InStr(itm.Subject , "yellow") > 0 Then
itm.Delete
End If
End Sub

Yes! - I see what you mean now, that works like a charm. Is there a way to switch it to "permanently delete" instead of just delete? Or both "mark as read" and then "delete".

With excel VBA I started learning by recording my macros and modifying the source code. There is no macro recorder for outlook, that's why I'm having a harder time with the syntax.

jperki39
10-19-2013, 02:28 PM
Yes! - I see what you mean now, that works like a charm. Is there a way to switch it to "permanently delete" instead of just delete? Or both "mark as read" and then "delete".

With excel VBA I started learning by recording my macros and modifying the source code. There is no macro recorder for outlook, that's why I'm having a harder time with the syntax.

I figured it out ... I modified it to be like this:


itm.UnRead = False
itm.Save
itm.Delete