Print Filtered Emails by Time Range

'This bit is called when Outlook opens, and it stays resident.
'It gets ahold of the inbox so it can tell when a new message is fetched from the server.

Private objNS As Outlook.NameSpace
Private WithEvents objNewMailItems As Outlook.Items
Private WithEvents objNewMTRSItems As Outlook.Items

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

Set objMyMTRSInbox = objNS.GetDefaultFolder(olFolderInbox).Parent.Folders("MaconTalqRedSonit")
Set objNewMTRSItems = objMyMTRSInbox.Items
End Sub


Private Sub objNewMRTSItems_ItemAdd(ByVal Item As Object)
 Item.PrintOut
End Sub



'Soli Deo Gloria
' (C) Mike Tulloch 2/16/22
' Prints ATM, ITM, Sonitrol, Talquin, MaconWater, and Redwire files,
' b/c all we do is print them anyway. Saves me from having to select
' and manually print them.
Private Sub CheckATMFiles(Msg As Outlook.MailItem)
Dim SixPM As Date
Dim SevenAM As Date

SixPM = TimeValue("06:00:00 PM")
SevenAM = TimeValue("07:00:00 AM")

'This prevents printing for 3rd shift ATM/ITM files
If (TimeValue(Now) < SevenAM) Then
    Exit Sub
ElseIf (TimeValue(Now) > SixPM) Then
    Exit Sub
End If

'Otherwise print it
Msg.PrintOut

End Sub



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
Dim SubjectStr As String
Dim BigSubjectStr As String

strIDs = Split(EntryIDCollection, ",")

On Error GoTo ErrorHandler
For intX = 0 To UBound(strIDs)
    Set objNS = Application.GetNamespace("MAPI")
    Set objEmail = objNS.GetItemFromID(strIDs(intX))
    If (TypeName(objEmail) = "MailItem") Then

  
    'This gets the ATM summary and ITM summary files
    SubjectStr = Left$(objEmail.Subject, 31)
    If (StrComp(SubjectStr, "RemoteScan - NewTweedy Server -", vbTextCompare) = 0) Then
        Call CheckATMFiles(objEmail)
    End If
  
    'This gets the deposits for ATM, ITM, Redwire, Sonitrol, XXXWater, and YYYY
    SubjectStr = Left$(objEmail.Subject, 36)
    If (StrComp(SubjectStr, "Deposit received by ZZZZ Little Rock", vbTextCompare) = 0) Then
        Call CheckATMFiles(objEmail)
    End If
  
    'This gets the XXX Water summary
    SubjectStr = Left$(objEmail.Subject, 23)
    If (StrComp(SubjectStr, "RemoteScan - XXXWater", vbTextCompare) = 0) Then
        Call CheckATMFiles(objEmail)
    End If
  
    'This gets the Sonitrol summary
    SubjectStr = Left$(objEmail.Subject, 21)
    If (StrComp(SubjectStr, "RemoteScan - Sonitrol", vbTextCompare) = 0) Then
        Call CheckATMFiles(objEmail)
    End If
  
    'This gets the Redwire summary
    SubjectStr = Left$(objEmail.Subject, 20)
    If (StrComp(SubjectStr, "RemoteScan - Redwire", vbTextCompare) = 0) Then
        Call CheckATMFiles(objEmail)
    End If
  
    'This gets the YYYY Electric summary
    SubjectStr = Left$(objEmail.Subject, 29)
    If (StrComp(SubjectStr, "RemoteScan - YYYY Electric", vbTextCompare) = 0) Then
        Call CheckATMFiles(objEmail)
    End If
  End If
Next

Set objEmail = Nothing

ProgramExit:
  Exit Sub

ErrorHandler:
  'Took this out b/c I don't care about the error - just fail silently.
  'MsgBox Err.Number & " - " & Err.Description
 Resume ProgramExit

End Sub