Excel VBA - process incoming Outlook emails

Hi

I was given the following code to process incoming emails from within Excel
but am not sure where to put it. I put it in the ThisWorkbook module and it didn't activate
Private Sub appOL_NewMailEx(ByVal EntryIDCollection As String)

   'YOUR CODE GOES IN HERE
   On Error GoTo EH
   
   Dim arrEID As Variant, varEID As Variant, olkItem As Object
   arrEID = Split(EntryIDCollection, ",")
   Dim Atmt As Outlook.Attachment
   Dim FileName As String
   Dim MYDOC_DIR As String: MYDOC_DIR = Environ("userprofile") & "\Documents"
    
    For Each varEID In arrEID
        Set olkItem = appOL.Session.GetItemFromID(varEID)
        If olkItem.Class = olMail Then
            If InStr(olkItem.Subject, "SMS Message received") > 0 Then
                'Your code goes here'
                    'For Each Atmt In olkItem.Attachments
                        'FileName = MYDOC_DIR & Atmt.FileName
                        'Atmt.SaveAsFile FileName
                    'Next Atmt
                MsgBox "Received"
           End If
         End If
    Next
    
    Set olkItem = Nothing
    Exit Sub
EH:
    MsgBox "There was an error processing an incoming email! " & Err.Description
    
End Sub

Open in new window

Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
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.

Chris BottomleySoftware Quality Lead EngineerCommented:
It's outlook code and belongs in thisoutlooksession but it doesn't sve to outlook as it is supplied

Chris
0
Chris BottomleySoftware Quality Lead EngineerCommented:
Beg pardon ... it looked like outlook code but I see now the name appOL_NewMailEx so perhaps it's not but I suggest it is called by thisoutlooksession.
0
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Hi Chris, I though this would work in Excel?
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Hi Chris. Sent reply before reading your second reply. So where exactly would I put that? In an Outlook event?
0
Rory ArchibaldCommented:
It needs to go in a class module - you appear to be have missed the key parts of the post where I gave you that! ;)
Suggest you go back and review it - you need that as a class, then code in the workbook_open to create a new instance of the class and assign an Outlook.Application instance to the appOL variable.
0

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
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thanks very much
0
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
Microsoft Excel

From novice to tech pro — start learning today.