VBA统计电子邮件模块!

发布于 2024-11-11 16:08:52 字数 1200 浏览 2 评论 0原文

这是我昨天发布的问题的后续问题。我觉得我已经非常接近在 VBA 中创建一个模块,该模块将计算一周中特定一天发送的电子邮件数量。目前选择的日期是星期一。

但是,代码尚未运行,Outlook 拒绝查看特定模块。
我确信其中有一些错误。如果有人能指出这些,我将不胜感激。
我还认为这样的代码对其他人来说可能很有用,以供将来参考,因为这种模块的代码似乎在互联网上不容易获得(我已经看过了!),但形成了一种许多人会使用的搜索参数类型发现有用!

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

与往事干杯 2024-11-18 16:08:52

调试时删除on error resume next
它隐藏了错误。如果有必要,您可以稍后将其放回去。
忽略错误并不是一个好主意,最好明确地处理错误。

让我印象深刻的一件事是:

For Each MapiItem In MapiFolderInbox.Messages
    If MapiItem.TimeReceived = Date Then
      Count = Count + 1
      Next MapiItem   
    End If

应该是

For Each MapiItem In MapiFolderInbox.Messages
  If MapiItem.TimeReceived = Date Then
    Count = Count + 1
  End If
Next MapiItem   

除此之外它对我来说看起来还不错。

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.

街道布景 2024-11-18 16:08:52

我认为收到的时间更像是一个时间戳,除了日期之外,它还有时间。你可能应该这样使用它;

For Each MapiItem In MapiFolderInbox.Messages   
    If  MapiItem.TimeReceived > YTS And MapiItem.TimeReceived < TTS Then
        Count = Count + 1   
    End If 
Next MapiItem

其中 YTS 和 TTS 是时间戳,其中包含昨天的时间戳和今天的时间戳,

例如 01:06:2011:23:59:00 和 02:06:2011:23:59:00

您应该通过调试代码来确认这一点。希望这有帮助。

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.

流绪微梦 2024-11-18 16:08:52

Outlook 仅在参数留空时才看到该模块...而不是包含(可选 dteDate 作为日期)。

除此之外,根据 Johan 的建议,模块运行,但只有 Count = 1。也就是说,无论输入如何,周一收到的电子邮件的结果始终为 1。

我也尝试了 adbanginwar 的建议,但在这种情况下,显示“Expected: Then or GoTo”的编译错误。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文