Move mail to a folder based on 2 different words in the subject fi

A

Alan Kirkham

When an e-mail arrives that has 2 specific words in the subject field, I
would like the mail to be moved to a personal archive folder. The 2 words are
not next to each other. For example, Subject field is "This is a test mail",
and the 2 words that I want to look for are "This" and "mail".

The script below is one that works if only 1 word is being checked and
renames the subject line.

Sub ChangeSubjectName(MyMail As MailItem)
Dim strID As String
Dim olNS As Outlook.NameSpace
Dim olMail As Outlook.MailItem

strID = MyMail.EntryID
Set olNS = Application.GetNamespace("MAPI")
Set olMail = olNS.GetItemFromID(strID)
' do stuff with olMail, e.g.

If olMail.Subject = "test" Then
olMail.Subject = "whatever you want the subject to be"
olMail.Save
End If

Set olMail = Nothing
Set olNS = Nothing
End Sub
 
E

Eric Legault [MVP - Outlook]

Use multiple InStr expressions to evaluate whether the desired words are
present in the Subject line and continue your logic if they all satisfy your
criteria.
 
A

Alan Kirkham

Thank you for your reply Eric. The InStr expression pushed me in the right
direction and I was able to solve my query. Below is the finished script.
Thanks again.

'***********************************

Sub MoveMail(MyMail As MailItem)
Dim strID As String
Dim olNS As Outlook.NameSpace
Dim olMail As Outlook.MailItem
Dim IMAPMoveMail As MAPIFolder
Dim Strtest1
Dim Strtest2

strID = MyMail.EntryID
Set olNS = Application.GetNamespace("MAPI")
Set olMail = olNS.GetItemFromID(strID)
Set objNS = Application.GetNamespace("MAPI")
Set IMAPMoveMail = objNS.Folders("Personal Intel").Folders("atest")

' do stuff with olMail, e.g.

Strtest1 = "test1"
Strtest2 = "test2"

If InStr(olMail.Subject, Strtest1) And InStr(olMail.Subject, Strtest2)
Then
olMail.Move IMAPMoveMail
'olMail.Save
End If

Set olMail = Nothing
Set olNS = Nothing
End Sub

'******************************************************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top