Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 673
  • Last Modified:

Excel/Outlook VBA - processing incoming emails

Hi

I have to distribute a spreadsheet application to over a hundred people.
Part of what it does is processes incoming emails. The code below is the Outlook code that
I have used on my development machine behind Outlook. How do I add code to the Excel spreadsheet that executes code when emails come in to process them in a similar way to the code below? I need this because I can't get to all the recipients to paste the code to Outlook
Private Sub Application_NewMailEx(ByVal EntryIDCollection As String)

On Error GoTo EH

   Dim arrEID As Variant, varEID As Variant, olkItem As Object
   arrEID = Split(EntryIDCollection, ",")
   Dim Atmt As Attachment
   Dim FileName As String
   Dim MYDOC_DIR As String: MYDOC_DIR = Environ("userprofile") & "\Documents"
    
    For Each varEID In arrEID
        Set olkItem = Session.GetItemFromID(varEID)
        If olkItem.Class = olMail Then
            If InStr(olkItem.Subject, "Drilling Data") > 0 Then
                'Your code goes here'
                    For Each Atmt In olkItem.Attachments
                        FileName = MYDOC_DIR & "\Lesedi Drilling Data\" & Convert_Email(olkItem.SenderEmailAddress) & "_" & Atmt.FileName
                        Atmt.SaveAsFile FileName
                    Next Atmt
    
               oLoop
            End If
        If InStr(olkItem.Subject, "New Holes") > 0 Then
                'Your code goes here'
                    For Each Atmt In olkItem.Attachments
                        FileName = MYDOC_DIR & "\Lesedi Drilling Data\New Holes\" & Convert_Email(olkItem.SenderEmailAddress) & "_" & Atmt.FileName
                        Atmt.SaveAsFile FileName
                    Next Atmt
    
               oNewHole
            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

0
Murray Brown
Asked:
Murray Brown
  • 5
  • 4
  • 3
1 Solution
 
Rory ArchibaldCommented:
Do they all have the same version of Outlook?
0
 
SiddharthRoutCommented:
>>> How do I add code to the Excel spreadsheet that executes code when emails come in to process them in a similar way to the code below?

AFAIK, You cannot do that. You cannot monitor the 'live' mails from Excel.

Sid
0
 
Rory ArchibaldCommented:
Sure you can! :)
0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
SiddharthRoutCommented:
Whoa! Then I am definitely interested :)

Sid
0
 
Rory ArchibaldCommented:
For example (assuming they all have the same OL version as I think NewMailEx is not available in all):
Private WithEvents appOL As Outlook.Application

Private Sub appOL_NewMailEx(ByVal EntryIDCollection As String)
   ' YOUR CODE GOES IN HERE
End Sub

Private Sub Workbook_Open()
   Set appOL = GetObject(, "Outlook.Application")
End Sub

Open in new window

0
 
SiddharthRoutCommented:
I am solving a question in another thread. After that I am gonna try that code. Looks interesting. :)

Sid
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thanks Rory. What code would I have there to look for a match in the header
0
 
Rory ArchibaldCommented:
Your code already does that doesn't it?
0
 
SiddharthRoutCommented:
Ok I am back.

A quick thought.

Excel has to remain open at all time for monitoring the email? Then that is the biggest drawback I guess?

I Googled on the code to monitor Outlook real time but couldn't find any code.

Sid
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thanks very much. Going to post a further related question. That worked really well
0
 
Rory ArchibaldCommented:
Your code would become something like:
Private Sub appOL_NewMailEx(ByVal EntryIDCollection As String)

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, "Drilling Data") > 0 Then
                'Your code goes here'
                    For Each Atmt In olkItem.Attachments
                        FileName = MYDOC_DIR & "\Lesedi Drilling Data\" & Convert_Email(olkItem.SenderEmailAddress) & "_" & Atmt.FileName
                        Atmt.SaveAsFile FileName
                    Next Atmt
    
               oLoop
            End If
        If InStr(olkItem.Subject, "New Holes") > 0 Then
                'Your code goes here'
                    For Each Atmt In olkItem.Attachments
                        FileName = MYDOC_DIR & "\Lesedi Drilling Data\New Holes\" & Convert_Email(olkItem.SenderEmailAddress) & "_" & Atmt.FileName
                        Atmt.SaveAsFile FileName
                    Next Atmt
    
               oNewHole
            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


You will need a reference set to Outlook.
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 5
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now