PDA

View Full Version : Outlook - Move emails based on condition



borudev
04-01-2011, 07:30 AM
Hey,

I need your help with Outlook VBA scripting. I'm not familiar with VBA at all, but I have experience with c++. So here's what I'm trying to accomplish:
I need a VBA script which would scan one folder(Can be inbox or any other) in Outlook, and move emails to another folder if it matches the following criteria:
if subject =,!=,like,not like "xxx"
AND
email_body like "xxx"
AND
has attachment = yes/no
AND
attachment is = *.pdf

So basically I have to write a rule set for to move emails from one folder to another if it match the criteria. There's going to be a lot of rules.

Does anyone know where I could find an example or maybe a code sample so I can try to adjust it so it meets my needs?

Any help is appreaciated.

Thanks

JP2112
04-04-2011, 01:41 PM
You'll need to be more specific about the conditions, but here is some starting code.

Sub DoSomething()
Dim selectedFolder As Outlook.MAPIFolder
Dim targetFolder As Outlook.MAPIFolder
Dim itms As Outlook.Items
Dim msg As Outlook.MailItem
Dim i As Long
Dim msgAttachs As Outlook.Attachments
' choose source folder
Set selectedFolder = Outlook.GetNamespace("MAPI").PickFolder
If selectedFolder Is Nothing Then Exit Sub
' choose target folder
Set targetFolder = Outlook.GetNamespace("MAPI").PickFolder
If targetFolder Is Nothing Then Exit Sub
Set itms = selectedFolder.Items
For i = itms.Count To 1 Step -1
If TypeName(itms.Item(i)) = "MailItem" Then
Set msg = itms.Item(i)
Set msgAttachs = msg.Attachments

' your conditions for moving the message should be here
If msgAttachs.Count > 0 Then
msg.Move targetFolder
End If
End If
Next i
End Sub

lsheetrit45
04-07-2011, 07:49 AM
Hey JP,

Any way to make this work for encrypted emails?:banghead:

Thanks!

JP2112
04-07-2011, 09:44 AM
No idea. I'm not sure if you can decrypt them programmatically, either.

ry4n
12-04-2015, 12:24 PM
simple but effective example.

thank you jp