Link to home
Start Free TrialLog in
Avatar of Murray Brown
Murray BrownFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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

Avatar of Chris Bottomley
Chris Bottomley
Flag of United Kingdom of Great Britain and Northern Ireland image

It's outlook code and belongs in thisoutlooksession but it doesn't sve to outlook as it is supplied

Chris
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.
Avatar of Murray Brown

ASKER

Hi Chris, I though this would work in Excel?
Hi Chris. Sent reply before reading your second reply. So where exactly would I put that? In an Outlook event?
ASKER CERTIFIED SOLUTION
Avatar of Rory Archibald
Rory Archibald
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks very much