Solved

Excel/Outlook VBA - processing incoming emails

Posted on 2011-03-11
12
636 Views
Last Modified: 2012-05-11
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
Comment
Question by:murbro
  • 5
  • 4
  • 3
12 Comments
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 35108995
Do they all have the same version of Outlook?
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35109035
>>> 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
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 35109083
Sure you can! :)
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35109093
Whoa! Then I am definitely interested :)

Sid
0
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 500 total points
ID: 35109102
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
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35109117
I am solving a question in another thread. After that I am gonna try that code. Looks interesting. :)

Sid
0
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 

Author Comment

by:murbro
ID: 35109550
Thanks Rory. What code would I have there to look for a match in the header
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 35109586
Your code already does that doesn't it?
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35109592
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
 

Author Closing Comment

by:murbro
ID: 35109600
Thanks very much. Going to post a further related question. That worked really well
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 35109607
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
 

Author Comment

by:murbro
ID: 35109647
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Are you unable to connect or configure Hotmail email account in Microsoft Outlook 2010, 2007? Or Outlook.com emails are not downloading to Outlook? Lets’ see the problem and resolve Outlook Connector error syncing folder hierarchy (0x8004102A).
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
CodeTwo Sync for iCloud (http://www.codetwo.com/sync-for-icloud?sts=6554) automatically synchronizes your Outlook 2016, 2013, 2010 or 2007 folders with iCloud folders available via iCloud Control Panel. This lets you automatically sync them with…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now