Run VBA script on email matching Outlook Rule
Hello,
Using scripts posted online, I have put together the code below which move a selected email from the inbox to a subfolder.
I have tied this script to a rule
""
Apply this rule after the message arrives
with Subject of Interest in the subject
and on this machine only
run Project1.RuleMoveToFolder
""
How can I edit the code below to run on the emails selected by the rule above instead of the email currently selected in the inbox? I have tried including an if statement to check the subject of the email before moving it to the folder but it didn't work (email with the correct subject is not moved to the folder).
Here is the code:
Code:
Sub RuleMoveToFolder(item As MailItem)
mailboxNameString = "Mailbox - First Name Last Name"
Dim olApp As New Outlook.Application
Dim olNameSpace As Outlook.NameSpace
Dim olCurrExplorer As Outlook.Explorer
Dim olCurrSelection As Outlook.Selection
Dim olDestFolder As Outlook.MAPIFolder
Dim m As Integer
Set olNameSpace = olApp.GetNamespace("MAPI")
Set olCurrExplorer = olApp.ActiveExplorer
Set olCurrSelection = olCurrExplorer.Selection
Set olDestFolder = olNameSpace.Folders(mailboxNameString).Folders("Inbox").Folders("Folder1").Folders("Subfolder1")
For m = 1 To olCurrSelection.Count
Set item = olCurrSelection.item(m)
If InStr(0, item.Subject, "Subject of Interest", vbTextCompare) > 0 Then
item.Move olDestFolder
End
Next m
End Sub
Could you please point me in the right direction to apply this code only to emails that get selected by the rule?
Thank you very much.
1 Attachment(s)
I've made some modifications
Quote:
Originally Posted by
gmayor
As it is not clear how your folders and accounts are arranged who not simply pick the destination folder also
Code:
Set olFolder = Application.Session.PickFolder
Set olDestFolder = Application.Session.PickFolder
Or you can Loop throughout the stores and select the store name which is the partially obscured 'dbable' etc then loop through the folders there
Code:
Sub Test()
Dim olStore As Store
Dim olDestFolder As Folder
For Each olStore In Application.Session.Stores
If olStore.DisplayName = "dbable...etc" Then
For Each olDestFolder In olStore.GetRootFolder.folders
If olDestFolder.Name = "Test" Then
MsgBox olDestFolder.Items.Count
Exit For
End If
Next olDestFolder
Exit For
End If
Next olStore
End Sub
Or if you are certain of the path of 'Test' which appears to be a direct sub folder of 'dbable...etc' then you can select it directly
Code:
Sub Test2()
Dim olDestFolder As Folder
Set olDestFolder = Application.Session.Stores("dbable... etc").GetRootFolder.folders("Test")
MsgBox olDestFolder.Items.Count
End Sub
All of those worked great thank you! :friends::bow::clap:
I do have another question if you don't mind.
I modified it to search through an Array; but I keep getting Run-Time error, the message you specified cannot be found.
Code:
Sub MoveMessages()Dim olFolder As Folder
Dim olDestFolder As Folder
Dim olItems As Outlook.Items
Dim t As Long, j As Long: j = 0
Dim arrTest As Variant
arrTest = Array("spring", "reaching out", "reviewing the data")
Set olFolder = Application.Session.Stores("dbable***x").GetRootFolder.Folders("Production")
Set olDestFolder = Application.Session.Stores("dbabler******").GetRootFolder.Folders("Test")
Set olItems = olFolder.Items
For t = olItems.Count To 1 Step -1
If TypeName(olItems(t)) = "MailItem" Then
For b = LBound(arrTest) To UBound(arrTest)
If InStr(olItems(t).Body, arrTest(b)) Then
olItems(t).Move olDestFolder
j = j + 1
Else
End If
Next b
End If
Next t
MsgBox j & " message items moved"
lbl_Exit:
Set olItems = Nothing
Set olFolder = Nothing
Set olDestFolder = Nothing
Exit Sub
End Sub
It will move a couple of messages then fail. What am I doing wrong?
Is it because I'm searching the body?
or is it because I am doing something wrong with my array...?
Here is a picture of my local window if that is helpful.
Attachment 22031
Thank you again in advance for your help, and for your past help :)