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?
 
Rory ArchibaldConnect With a Mentor Commented:
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
 
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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Hi Chris, I though this would work in Excel?
0
 
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
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thanks very much
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.