Check email address for specific text if sent from specific email address

Hello
I'm looking for a Vba script to run on outlook everytime a message arrives.
If would check subject line if sent from email123@gmail.com and doesn't have the code 338 on the subject it would add [338] at the beginning of the email.
Thank you very much
Joao MaviosoAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Claire StrebSoftware GuruCommented:
Option Explicit
Public Sub Confidential()
    Dim Item As Outlook.MailItem
    Dim oInspector As Inspector

    Set oInspector = Application.ActiveInspector

    If oInspector Is Nothing Then
        Set Item = Application.ActiveExplorer.Selection.Item(1)
    Else
       Set Item = oInspector.CurrentItem
    End If

    If Item.From = "email123@gmail.com" Then
        Item.Subject = "[338]" & Item.Subject
        Item.Save
    End If

    Set Item = Nothing
    Set oInspector = Nothing

End Sub

Open in new window

Joao MaviosoAuthor Commented:
Hello,
Thank you for your input, almost works, but:
Run-time error '438':
Object doesn't support this property or method

On this code:
If Item.From = "email1234@gmail.com" Then

Any sugestion?
Claire StrebSoftware GuruCommented:
Oops!  Aircoding.

Please try Item.SenderEmailAddress instead of Item.From.

(https://msdn.microsoft.com/en-us/vba/outlook-vba/articles/mailitem-object-outlook)

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
Joao MaviosoAuthor Commented:
That worked :)
Thank you.

Any tip on how to work with a text file where I could store many codes, like 338, 339, 340 and corresponding emails addresses?

I believe I would have to change them with vars and do some sort of loop through the text file, being this last part the bigger problem....
Claire StrebSoftware GuruCommented:
I would have a text file like this:
338|email1@abc.com
339|email2@def.com
340|emsil3@ghi.com

Set a reference to MS Scripting runtime ('Microsoft Scripting Runtime')

Dim dict As New Scripting.Dictionary

Then loop through the lines of the file until EOF is reached:
    Read each line, split it by | into myArray, and
    If Not dict.Exists(myArray(0)) Then
        dict.Add myArray(0), myArray(1)
    End If

Be sure to Set dict = Nothing after the loop

Then you can simply refer to dict(myKey) to get the email address, where myKey is 338, 339, 340, etc.

Hope that helps,
Claire
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.