You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:统计Outlook收件箱中标记为“已完成”的邮件数量

Count Completed Emails in Outlook Inbox

Looks like you're halfway there! To count only the items marked as "Completed" in your Outlook Inbox, we need to add logic to check each email's completion status and tally those that qualify. Here's the modified and complete code:

Sub CountCompletedEmails()
    Dim objOutlook As Object, objnSpace As Object, objFolder As MAPIFolder
    Dim EmailCount As Integer
    Dim objItem As Object
    Dim objMail As MailItem
    
    ' Initialize Outlook objects
    Set objOutlook = CreateObject("Outlook.Application")
    Set objnSpace = objOutlook.GetNamespace("MAPI")
    
    ' Locate the target folder
    On Error Resume Next
    Set objFolder = objnSpace.Folders("Investment Central").Folders("Inbox")
    If Err.Number <> 0 Then
        Err.Clear
        MsgBox "No such folder exists. Please check the folder name.", vbExclamation
        Exit Sub
    End If
    On Error GoTo 0 ' Disable error resume to catch other issues
    
    EmailCount = 0
    
    ' Loop through each item in the Inbox
    For Each objItem In objFolder.Items
        ' Only process MailItems (skip meetings, tasks, etc.)
        If TypeName(objItem) = "MailItem" Then
            Set objMail = objItem
            ' Check if the email is marked as completed (TaskCompletedDate is set)
            If Not objMail.TaskCompletedDate Is Nothing Then
                EmailCount = EmailCount + 1
            End If
        End If
    Next objItem
    
    ' Show the final count
    MsgBox "Total completed emails in Inbox: " & EmailCount, vbInformation
    
    ' Clean up resources
    Set objMail = Nothing
    Set objItem = Nothing
    Set objFolder = Nothing
    Set objnSpace = Nothing
    Set objOutlook = Nothing
End Sub

Key Changes Explained:

  • Item Type Check: We verify each item is a MailItem to avoid errors with non-email items (like calendar invites or tasks) that might be in your Inbox.
  • Completion Status Check: Outlook uses the TaskCompletedDate property to track when an email is marked as completed. If this property isn't empty, the email is considered done.
  • Improved Error Handling: We turn off On Error Resume Next after locating the folder to catch any unexpected issues during the item loop.
  • Resource Cleanup: Explicitly releasing objects helps free up memory after the macro runs.

Notes:

  • If your mailbox uses a different name than "Investment Central", update that string to match your actual mailbox name.
  • This code only checks the main Inbox folder. If you need to include subfolders, we'd need to add a recursive function to traverse them.

内容的提问来源于stack exchange,提问作者A.Richter

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:45:44