Si1209
09-15-2016, 03:57 AM
So someone, who has left my work, created this code to count emails in specified mailboxes:-
Sub NWHFX2()
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 - $north west halifax").Folders("Inbox")
If Err.Number <> 0 Then
Err.Clear
''MsgBox "No such folder."
Exit Sub
End If
' Today - 3 Count in NW HFX''''''''''''''''''
Dim iCount As Integer, DateCount As Integer
Dim myDate As Date
EmailCount = objFolder.Items.Count
DateCount = 0
myDate = Sheets("Sheet1").Range("f9").Value
For iCount = 1 To EmailCount
With objFolder.Items(iCount)
If DateSerial(Year(.ReceivedTime), Month(.ReceivedTime), Day(.ReceivedTime)) = myDate Then DateCount = DateCount + 1
End With
Next iCount
Cells(10, 6).Value = DateCount
' Today - 4 Count in NW HFX'''''''''''''''''
DateCount = 0
myDate = Sheets("Sheet1").Range("e9").Value
For iCount = 1 To EmailCount
With objFolder.Items(iCount)
If DateSerial(Year(.ReceivedTime), Month(.ReceivedTime), Day(.ReceivedTime)) = myDate Then DateCount = DateCount + 1
End With
Next iCount
Cells(10, 5).Value = DateCount
' Today - 5 Count NW HFX'''''''''''''''''
DateCount = 0
myDate = Sheets("Sheet1").Range("d9").Value
For iCount = 1 To EmailCount
With objFolder.Items(iCount)
If DateSerial(Year(.ReceivedTime), Month(.ReceivedTime), Day(.ReceivedTime)) = myDate Then DateCount = DateCount + 1
End With
Next iCount
Cells(10, 4).Value = DateCount
End Sub
Sub NWHFX3()
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 - $north west halifax").Folders("Inbox")
If Err.Number <> 0 Then
Err.Clear
''MsgBox "No such folder."
Exit Sub
End If
' Today - 6 Count in NW HFX''''''''''''''''''
Dim iCount As Integer, DateCount As Integer
Dim myDate As Date
EmailCount = objFolder.Items.Count
DateCount = 0
myDate = Sheets("Sheet1").Range("c9").Value
For iCount = 1 To EmailCount
With objFolder.Items(iCount)
If DateSerial(Year(.ReceivedTime), Month(.ReceivedTime), Day(.ReceivedTime)) = myDate Then DateCount = DateCount + 1
End With
Next iCount
Cells(10, 3).Value = DateCount
End Sub
Most of the time the code runs fine and works and pulls through the correct information, however sometimes, like today when you run the macro and it imports data in to the Excel table it decides to put 1 email in for the data in cell 10,3. However, when this is checked no emails in the inbox are dated for that day, the date referrenced in cell 10,3 being 5 days prior to today so the dates & days will change on a daily basis.
Is this an issue with the code or is it something within Outlook that you know of that may be causing this issue?
Sub NWHFX2()
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 - $north west halifax").Folders("Inbox")
If Err.Number <> 0 Then
Err.Clear
''MsgBox "No such folder."
Exit Sub
End If
' Today - 3 Count in NW HFX''''''''''''''''''
Dim iCount As Integer, DateCount As Integer
Dim myDate As Date
EmailCount = objFolder.Items.Count
DateCount = 0
myDate = Sheets("Sheet1").Range("f9").Value
For iCount = 1 To EmailCount
With objFolder.Items(iCount)
If DateSerial(Year(.ReceivedTime), Month(.ReceivedTime), Day(.ReceivedTime)) = myDate Then DateCount = DateCount + 1
End With
Next iCount
Cells(10, 6).Value = DateCount
' Today - 4 Count in NW HFX'''''''''''''''''
DateCount = 0
myDate = Sheets("Sheet1").Range("e9").Value
For iCount = 1 To EmailCount
With objFolder.Items(iCount)
If DateSerial(Year(.ReceivedTime), Month(.ReceivedTime), Day(.ReceivedTime)) = myDate Then DateCount = DateCount + 1
End With
Next iCount
Cells(10, 5).Value = DateCount
' Today - 5 Count NW HFX'''''''''''''''''
DateCount = 0
myDate = Sheets("Sheet1").Range("d9").Value
For iCount = 1 To EmailCount
With objFolder.Items(iCount)
If DateSerial(Year(.ReceivedTime), Month(.ReceivedTime), Day(.ReceivedTime)) = myDate Then DateCount = DateCount + 1
End With
Next iCount
Cells(10, 4).Value = DateCount
End Sub
Sub NWHFX3()
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 - $north west halifax").Folders("Inbox")
If Err.Number <> 0 Then
Err.Clear
''MsgBox "No such folder."
Exit Sub
End If
' Today - 6 Count in NW HFX''''''''''''''''''
Dim iCount As Integer, DateCount As Integer
Dim myDate As Date
EmailCount = objFolder.Items.Count
DateCount = 0
myDate = Sheets("Sheet1").Range("c9").Value
For iCount = 1 To EmailCount
With objFolder.Items(iCount)
If DateSerial(Year(.ReceivedTime), Month(.ReceivedTime), Day(.ReceivedTime)) = myDate Then DateCount = DateCount + 1
End With
Next iCount
Cells(10, 3).Value = DateCount
End Sub
Most of the time the code runs fine and works and pulls through the correct information, however sometimes, like today when you run the macro and it imports data in to the Excel table it decides to put 1 email in for the data in cell 10,3. However, when this is checked no emails in the inbox are dated for that day, the date referrenced in cell 10,3 being 5 days prior to today so the dates & days will change on a daily basis.
Is this an issue with the code or is it something within Outlook that you know of that may be causing this issue?