Hi, I have an issue. I have a script that is very basic in my opinion, it looks at all the emails in a certain folder, checks the Subject for a keyword and based on this files it in a specific folder. Everything works perfect except, out of 10 messages - for example - it only processes 5-7 of them. If I run the macro again, it does a few more, etc. Eventually all are processed but I don't understand what I'm doing wrong that it doesn't cycle through all of them. I was hoping someone could take a look at this code to see where I might be going wrong. This code is from a much larger script but this is the important part related to the issue.
Code:
Public appOl As New Outlook.Application
Public ns As Outlook.NameSpace
Public Inbox As Outlook.MAPIFolder
Public DestFolder As Outlook.MAPIFolder
Public Atmt As Outlook.Attachment
Public olApp As Outlook.Application
Public olNewMail As Outlook.MailItem
Public Item As Object
Private Sub Check_Support_Mailbox()
On Error GoTo Check_Support_Mailbox_err
Dim subText As String
Set ns = appOl.GetNamespace("MAPI")
Set Inbox = ns.Folders("Mailbox - MyName").Folders("Inbox")
'
For Each Item In Inbox.Items.Restrict("[UnRead] = True")
' Added this flag to see if it was looking at each msgs
' Based on this flag, it is not touching every email
Item.FlagIcon = olYellowFlagIcon
If (Item.Class = olMail) And (Item.UnRead) Then
'
' I removed the reference to all states, 3 is a good sampling
'
If InStr(Item.Subject, "AL -") 0 Then NorthIncidents
If InStr(Item.Subject, "AZ -") 0 Then WestIncidents
If InStr(Item.Subject, "AR -") 0 Then EastIncidents
End If
Next Item
Check_Support_Mailbox_exit:
Set Atmt = Nothing
Set Item = Nothing
Set ns = Nothing
Set appOl = Nothing
Exit Sub
Check_Support_Mailbox_err:
MsgBox "Error has occurred " & Err.Number & “: “ & Err.Description, vbCritical, "Error!"
Resume Process_Report_Emails_exit
End Sub
Also, maybe someone could offer a better solution for how I'm handling the keyword search? Basically, I'm looking for the STATE abbreviation in the subject line and based on that files it appropriately. Obviously 50 IfThen statements makes no sense but I just couldn't figure a better way.
THANKS FOR ANY ASSISTANCE!!!