I am trying to create a spreadsheet in Excel that when you run a button it will scan your mailbox for emails older than a specific time and produce a count of how many there are.
I already have a script that will run and count how many total are in the mailbox and how many in the mail box are not read. But I'd like to have this also calculate the above.
Any VBA + Outlook experts?
Code:
Sub countMail()
Dim objoutlook As Object, objnSpace As Object, objFolder As Object
Dim EmailCount As Integer
Set objoutlook = CreateObject("Outlook.Application")
Set objnSpace = objoutlook.GetNamespace("MAPI")
On Error Resume Next
Set objFolder = objnSpace.Folders("mailbox").Folders("Inbox")
If Err.Number <> 0 Then
Err.Clear
MsgBox "No such folder."
Exit Sub
End If
unreadCount = objFolder.UnReadItemCount
rcvdEmailCount = objFolder.Items.Count
shiftDayStartAMER = Date
shiftTimeStartAMER = " 7:45:00 AM"
shiftAMER = shiftDayStartAMER & shiftTimeStartAMER
ThisWorkbook.Sheets("Sheet1").Activate
Cells(2, 2).Value = rcvdEmailCount
Cells(4, 2).Value = unreadCount
End Sub