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 Since outlook connector update unable to send mail Version 14.0.5117.5000 Errors: 4102&amp; parsing MTOM Using Outlook 2
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
C Multiple emails Using Outlook 2
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
C Macro to extract sender name & subject line of incoming emails to single txt file Outlook VBA and Custom Forms 3
M Sync 'On my computer' emails Using Outlook 1
A Apply Selected Emails to outlook rules and Run Rules Using Outlook 5
A Run-time error '430' on certain emails when trying to set "Outlook.mailitem" as "ActiveExplorer.Selection.Item" Outlook VBA and Custom Forms 2
J Moved many emails to Outlook external folder, need to delete on Gmail server Using Outlook 14
P Move emails between 2 mailboxes. Using Outlook 0
R Assign Categories "Round Robin" style but in a shared mailbox but on specific emails only Outlook VBA and Custom Forms 8
P Outlook for Mac - emails are not sent out beginning last week Using Outlook 1
M Missing emails in Inbox, but are present in webmail Using Outlook 0
A How To Get Body of All Emails to view in Blue Using Outlook 1
A How to get body of all emails in outlook 2016 to view in blue color Using Outlook 1
N Extract Outlook emails to excel Outlook VBA and Custom Forms 2
M Group of emails Using Outlook 1
I Saving attachments from multiple emails and updating file name Outlook VBA and Custom Forms 0
R List folders in a combo box + select folder + move emails from inbox to that folder + reply to that email Outlook VBA and Custom Forms 1
D Assign categories to outgoing emails Outlook VBA and Custom Forms 0
J Outlook 2016 Moving IMAP emails to Exchange Using Outlook 1
P Flagging emails in conversations Using Outlook 2
V Outlook 2016 will not move emails in search results Using Outlook 4
D Outlook 2016 Deleting emails without affecting folder structure - Outlook Office 365 Using Outlook 3
M Filter Conversations - Filter Out Single Emails Outlook VBA and Custom Forms 3
F not receiving emails until i re-login Using Outlook 2
K No emails in inbox Using Outlook.com accounts in Outlook 1
A Record opened emails for reopening later Outlook VBA and Custom Forms 16
N VBA to delete duplicates by message-id on common pst for 2 or more emails Outlook VBA and Custom Forms 0
R Deleting emails from storage folder Outlook VBA and Custom Forms 1
R Outlook for Mac - Advanced Search and Tracking Sent Emails Using Outlook 0
DoctorJellybean Outlook 365 doesn't always mark emails as read Using Outlook 3
N Sent emails not reflected in all IMAP Folders Using Outlook 4
A Custom VBA to sort emails into folders Outlook VBA and Custom Forms 0
L Moving emails with similar subject and find the timings between the emails using outlook VBA macro Outlook VBA and Custom Forms 1
S How can I delete all copies of a given companies logo graphic from all my emails at once? Using Outlook 3
Y Open and Save Hyperlink Files in multiple emails Outlook VBA and Custom Forms 9
L Time format in Outlook calendar and emails Using Outlook 0
J Set Timer/Countdown on Emails? Outlook VBA and Custom Forms 3
T Render Outlook emails in MS access as they appear in Outlook Outlook VBA and Custom Forms 2
O Delete duplicate emails - subscription notifications Using Outlook 5
N Outlook rules don't create a copy for bcc'ed emails Using Outlook 3
R Can not create folder to store specific emails in in Outlook for Mac Using Outlook 1
V Emails remain in Drafts folder even though they have been sent Using Outlook 4
B Vba to monitor time to respond to emails using a shared mailbox Outlook VBA and Custom Forms 5

Similar threads

Top