This is a follow-up question of one which I posted yesterday. I feel I am getting quite close to creating a module in VBA that will count the number of e-mails sent on a particular day of the week. For the moment the day chosen is Monday.
However, the code is not yet working, and Outlook refuses to see the particular module.
I am sure there are a couple of errors in it. If someone could point these out, I would greatly appreciate it.
I also think that such code could be useful for others for future reference as the code for this kind of module does not seem to be readily available on the internet (I've looked!) and yet forms a type of search parameter that many will find useful!
Sub Count2(Optional dteDate As Date)
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("My Personal Emails").Folders("spam")
If Err.Number <> 0 Then
Err.Clear
MsgBox "No such folder."
Exit Sub
End If
Select Case Weekday(dteDate)
Case vbMonday
dteDate = Date
End Select
For Each MapiItem In MapiFolderInbox.Messages
If MapiItem.TimeReceived = Date Then
Count = Count + 1
Next MapiItem
End If
EmailCount = objFolder.Items.Count
Set objFolder = Nothing
Set objnSpace = Nothing
Set objOutlook = Nothing
MsgBox "Number of emails in the folder: " _
& EmailCount, , "Number of spam messages sent on a Monday: " & Count
End Sub
Whilst debugging drop the on error resume next
.
It hides the errors. You can put it back later if you have to.
It's not a great idea to ignore errors, better to handle the errors explicitly.
One thing that struck me:
For Each MapiItem In MapiFolderInbox.Messages
If MapiItem.TimeReceived = Date Then
Count = Count + 1
Next MapiItem
End If
Should be
For Each MapiItem In MapiFolderInbox.Messages
If MapiItem.TimeReceived = Date Then
Count = Count + 1
End If
Next MapiItem
Other than that it looks OK to me.
i think time received is more of a timestamp where in addition to date it would have time too. you should probably use it like this;
For Each MapiItem In MapiFolderInbox.Messages
If MapiItem.TimeReceived > YTS And MapiItem.TimeReceived < TTS Then
Count = Count + 1
End If
Next MapiItem
where YTS and TTS are timestamps where it would have yesterday's timestamp and today timestamp
for eg 01:06:2011:23:59:00 and 02:06:2011:23:59:00
you should comfirm this by debuging your code. hope this helps.
Outlook only sees the module when the parameters are left empty... as opposed to containing (Optional dteDate As Date).
Other than that, following the suggestion by Johan, the Module runs, but only ever has Count = 1. That is, that the result of emails received on a Monday is always 1 regardless of the input.
I also tried adbanginwar's suggestion, but in this case a compile error of 'Expected: Then or GoTo' is displayed.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.