Parsing All Emails

Status
Not open for further replies.

Guy

Member
Outlook version
Outlook 2016 32 bit
Email Account
Exchange Server
I have an Outlook client that is always running, on a machine always logged in. It uses a special email address and it's not a person. We use this email as a "traffic cop" that captures inbound mail from disparate business systems inside the building and then through VBA executes certain procedures, for example it will read the incoming mail and based on the subject go off in one case write an XML file to disk on a remote server that will be absorbed into another business system. We use the client as "glue" for integrating these systems. So far so good.

I have one routine that reads all inbound emails and then based on subject line (or lack of) content branches the action to take. I have in this routine a For Each (For Each InboxItem In olFolder.Items) statement that picks through the subject line. My question is will this routine look at multiple simultaneously received emails one after the other? I cannot easily test this scenario and want to make the tool robust. Does the For Each InboxItem step through one new email and then the next or will it just do the first one? Is there a better way to do what I am attempting here? I trigger this routine via a call from ThisOutlookSession using Sub Application_NewMail. Any insights here would be most appreciated.
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
For each will run through all messages in the folder. Not a problem if you move the messages out after processing (you'll only have new messages in the inbox), but is a problem if you have 100's of messages.

You want to use newmail or item add to process the current message. Hand it off to another macro to finish processing if you can - use

sub processmail(ByVal Item As Object)
' do whatever
if item.subject = "Howdy" then
end if
end sub

BTW, newmail is better than itemadd if the messages are coming in fast - if a large number of items are added to a folder at that same time, the ItemAdd event may not fire.
 

Guy

Member
Outlook version
Outlook 2016 32 bit
Email Account
Exchange Server
Thanks Diane, this helped a lot. I did fail to mention that the email frequency will be quite low. 5 per day max. Maybe 2 at once per server request kind of thing. Really like this forum - it has been a huge help over the years!
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
That is definitely slow enough for itemadd, but if newmail is working, no need to break what isn't broke. :) But, even with low volume, there can be benefit to handing the message off to a separate macro to process. If you are looking at multiple conditions and actions, the newmail macro can use if statements and push off to different macros. This can make it easier to troubleshoot if you need to edit or think there is a bug.
 

Guy

Member
Outlook version
Outlook 2016 32 bit
Email Account
Exchange Server
Thanks again Diane.

This all seems to be working nicely, however there is one spot that I find very odd that I have had to put into the code and that is getting the body text. If I don't display the email (InboxItem.Dsplay) and then pause the routine (Pause 1) VBA seems to "miss" the BodyText=InboxItem.Body call. The variable ends up empty if I remove these. I have to believe there is a better way than showing an email and pausing. Seems arcane to me. Thoughts? Other than that, this all seems to work quite nicely.

So here is my code basics for this:

Private Sub Application_NewMail()
Call ReadNewEMail
Call EmptyTrash
End Sub

Sub ReadNewEMail()

Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Dim olFolder As Outlook.MAPIFolder
Dim TEAMParsed As Outlook.folder
Dim FailedSubjectLine As Outlook.folder
Dim InboxItem As Object
Dim TrashBox As Object
Dim NoECONumber As Object
Dim WorkReqType As String

Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
Set olFolder = objNS.GetDefaultFolder(olFolderInbox)
Set olFolder = GetFolder("\\TEAM\Inbox")
Set TEAMParsed = olFolder.Folders("Parsed")
Set FailedSubjectLine = olFolder.Folders("Trash")
Set NoECONumber = olFolder.Folders("No ECO Number")

For Each InboxItem In olFolder.Items

strID = InboxItem.EntryID
Set InboxItem = Application.Session.GetItemFromID(strID)
InboxItem.Display 'DISPLAY EMAIL TO ALLOW CAPTURING BODY TEXT
Pause 1
BodyText = InboxItem.Body
InboxItem.Close olSave
SubjectLineText = InboxItem.Subject
EmailType = Left(SubjectLineText, 9)


Select Case EmailType 'I use this as the branching method to go off to other routines based on email subject line
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
This is kind of what you need to be doing:
Private Sub Application_Startup()
Dim objMyInbox As Outlook.MAPIFolder

Set objNS = Application.GetNamespace("MAPI")
Set objMyInbox = objNS.GetDefaultFolder(olFolderInbox)
Set objNewMailItems = objMyInbox.Items
Set objMyInbox = Nothing
End Sub


Private Sub objNewMailItems_ItemAdd(ByVal Item As Object)
'Ensure we are only working with e-mail items
If Item.Class <> olMail Then Exit Sub

Debug.Print "Message subject: " & Item .Subject
Debug.Print "Message sender: " & Item .SenderName & " (" & Item .SenderEmailAddress & ")";

ReadNewEMail item
EmptyTrash item

End Sub


Sub ReadNewEMail(ByVal Item As Object)

Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Dim olFolder As Outlook.MAPIFolder
Dim TEAMParsed As Outlook.folder
Dim FailedSubjectLine As Outlook.folder
Dim Item As Object
Dim TrashBox As Object
Dim NoECONumber As Object
Dim WorkReqType As String

Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")

' these can be set in the startup folder instead although with only a couple of messages per day being processed, it really doesnt matter
Set olFolder = GetFolder("\\TEAM\Inbox")
Set TEAMParsed = olFolder.Folders("Parsed")
Set FailedSubjectLine = olFolder.Folders("Trash")
Set NoECONumber = olFolder.Folders("No ECO Number")

strID = Item.EntryID
Item.Display 'DISPLAY EMAIL TO ALLOW CAPTURING BODY TEXT
Pause 1
BodyText = Item.Body
Item.Close olSave
SubjectLineText = Item.Subject
EmailType = Left(SubjectLineText, 9)
' reset of code


Using the newmailex method, you'd do something like this instead of the itemadd method -

Private Sub Application_NewMailEx(ByVal EntryIDCollection As
String)
Dim objNS As Outlook.NameSpace
Dim objEmail As Outlook.MailItem
Dim strIDs() As String
Dim intX As Integer
strIDs = Split(EntryIDCollection, ",")
For intX = 0 To UBound(strIDs)
Set objNS = Application.GetNamespace("MAPI")
Set objEmail = objNS.GetItemFromID(strIDs(intX))
Debug.Print "Message subject: " & objEmail.Subject
Debug.Print "Message sender:" & objEmail.SenderName &" (" & objEmail.SenderEmailAddress & ")"

ReadNewEMail objEmail
EmptyTrash objEmail

Next


Set objEmail = Nothing
End Sub
 

Guy

Member
Outlook version
Outlook 2016 32 bit
Email Account
Exchange Server
Awesome! I knew there had to be a better way. Terrific. Thank you.
 

Guy

Member
Outlook version
Outlook 2016 32 bit
Email Account
Exchange Server
Diane, thank you very much. I have made adjustsment per your suggestions and it runs perfectly. I have also read through your suggested readings. Very helpful and informative. Much appreciated.
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
J Email Parsing VBA Script for Outlook - NEEDED Outlook VBA and Custom Forms 7
J Outlook is not parsing html email, unreadable and attachment not decoded Using Outlook 1
S ->[O2007] Parsing each line of a MailItem HTMLBody? Outlook VBA and Custom Forms 2
F Managing and parsing names other than main contact Outlook VBA and Custom Forms 2
J Outlook 365 Emails showing as links and text only Using Outlook 4
G Removing old emails when adding accounts Using Outlook 3
N VBA Macro To Save Emails Outlook VBA and Custom Forms 1
Horsepower Lost emails Using Outlook 4
P Emails assigned with a certain category (within a shared inbox) to be copied to a specific folder. Outlook VBA and Custom Forms 2
M Saving emails using Visual Basic - Selecting folder with msoFileDialogFolderPicker Outlook VBA and Custom Forms 6
L Emails being archived when I press the A key accidentally - how do I stop this? Using Outlook 0
N Save emails within a certain date range to network drive Outlook VBA and Custom Forms 0
M I cant send emails via Outlook in my W10 PC. Using Outlook 3
e_a_g_l_e_p_i Can emails from Gmail be deleted when they are downloaded to Outlook 2010 Using Outlook 1
D Wrong email address in Outlook 2003 "From" tab in new outgoing emails Using Outlook 4
witzker How to find all emails from and to a contact in OL 2019 Using Outlook 6
S How to find emails that I sent that have not received a reply? Using Outlook 7
M Extract "Date sent" from emails (saved to folder using drag and drop) Outlook VBA and Custom Forms 1
R Sent emails show iCloud mail account not the alias Using Outlook 2
K Run a script rule to auto 'send again' on undeliverable emails? Outlook VBA and Custom Forms 1
J how to stop junk emails from *.onmicrosoft.com ? Using Outlook 2
FryW Need help modifying a VBA script for in coming emails to auto set custom reminder time Outlook VBA and Custom Forms 0
S Auto forward for multiple emails Outlook VBA and Custom Forms 0
M All fonts in Outlook emails display with exaggerated character spacing Using Outlook 3
L Images not displaying in emails sent to self Using Outlook 2
C Icons (arrows) not showing on emails forwarded or replied to Using Outlook 8
R Moved 6 months worth (approx 1500 emails) lost from moving from TPG inbox to Icloud inbox (folders) Using Outlook 3
U Approving Emails from iPhone Using Outlook 0
D cannot view emails in View pane (in the right pane), I only see one line or nothing Using Outlook 21
A Macro to file emails into subfolder based on subject line Outlook VBA and Custom Forms 1
N Help creating a VBA macro with conditional formatting to change the font color of all external emails to red Outlook VBA and Custom Forms 5
B Outlook 2016 Unable to view images or logos on the outlook 2016 emails the same html code works well when i use outlook 2010 Using Outlook 0
A Record opened emails for reopening later Using Outlook 2
Y Filter unread emails in a search folder vba help Outlook VBA and Custom Forms 0
S Outlook 2016 Change how Outlook shows me contacts in emails Using Outlook 0
T Outlook creating unwanted tasks in Tasks and Todo from emails Using Outlook 1
B Outlook 2016 Outlook crashes when trying to print certain emails Using Outlook 5
L IMAP Emails Disappear Using Outlook 0
L Need help modifying a VBA script for emails stuck in Outbox Outlook VBA and Custom Forms 6
S Macro to extract and modify links from emails Outlook VBA and Custom Forms 3
D Duplicate Emails on Phone and Tablet Using Outlook 0
S Macro for Loop through outlook unread emails Outlook VBA and Custom Forms 2
Globalforester ItemAdd Macro - multiple emails Outlook VBA and Custom Forms 3
R Auto Assign Category colours to Incoming Emails based on whom the email is addressed Outlook VBA and Custom Forms 3
P Forwarding emails issue with special characters replacing text body Using Outlook 1
B Emails get stuck in Outbox of Gmail IMAP in Outlook for Windows Using Outlook 0
C Multiple emails Using Outlook 8
Phillip Bit.ly emails Using Outlook 1
E Work uses live accounts for emails for all employees. Can we use the outlook calendar to give the employees an universal work schedule calendar? Using Outlook 1
G Alerting on missed emails in conversation Outlook VBA and Custom Forms 1

Similar threads

Top