2012-02-08 33 views
5

Tôi có mã sau để đếm số lượng email trong thư mục outlook.Đếm email theo triển vọng theo ngày

Sub HowManyEmails() 
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("Personal Folders").Folders("Inbox").Folders("report's").Folders("Customer")  
    If Err.Number <> 0 Then  
    Err.Clear 
    MsgBox "No such folder."  
    Exit Sub  
    End If 

EmailCount = objFolder.Items.Count  
Set objFolder = Nothing  
Set objnSpace = Nothing  
Set objOutlook = Nothing 

MsgBox "Number of emails in the folder: " & EmailCount, , "email count" End Sub 

Tôi đang cố đếm số email trong thư mục này theo ngày để tôi kết thúc đếm mỗi ngày.

+0

Đây rõ ràng isnt [tag: VBScript] - Ý anh là VBA từ bên trong Outlook? – brettdj

+0

Có thể dễ dàng liên kết với Excel hơn hoặc sử dụng ADO để chạy truy vấn: http://support.microsoft.com/kb/275262 – Fionnuala

Trả lời

10

Bạn có thể thử nó với mã này:

Sub HowManyEmails() 

    Dim objOutlook As Object, objnSpace As Object, objFolder As MAPIFolder 
    Dim EmailCount As Integer 
    Set objOutlook = CreateObject("Outlook.Application") 
    Set objnSpace = objOutlook.GetNamespace("MAPI") 

     On Error Resume Next 
     Set objFolder = objnSpace.Folders("Personal Folders").Folders("Inbox").Folders("report's").Folders("Customer") 
     If Err.Number <> 0 Then 
     Err.Clear 
     MsgBox "No such folder." 
     Exit Sub 
     End If 

    EmailCount = objFolder.Items.Count 

    MsgBox "Number of emails in the folder: " & EmailCount, , "email count" 

    Dim dateStr As String 
    Dim myItems As Outlook.Items 
    Dim dict As Object 
    Dim msg As String 
    Set dict = CreateObject("Scripting.Dictionary") 
    Set myItems = objFolder.Items 
    myItems.SetColumns ("SentOn") 
    ' Determine date of each message: 
    For Each myItem In myItems 
     dateStr = GetDate(myItem.SentOn) 
     If Not dict.Exists(dateStr) Then 
      dict(dateStr) = 0 
     End If 
     dict(dateStr) = CLng(dict(dateStr)) + 1 
    Next myItem 

    ' Output counts per day: 
    msg = "" 
    For Each o In dict.Keys 
     msg = msg & o & ": " & dict(o) & " items" & vbCrLf 
    Next 
    MsgBox msg 

    Set objFolder = Nothing 
    Set objnSpace = Nothing 
    Set objOutlook = Nothing 
End Sub 

Function GetDate(dt As Date) As String 
    GetDate = Year(dt) & "-" & Month(dt) & "-" & Day(dt) 
End Function 
+0

Cảm ơn công việc tuyệt vời, một câu hỏi là có cách nào để thông tin này được lưu vào tệp csv? – Shaun07776

+2

@fmunkert: +1 Được thực hiện tốt nhất :) Chỉ một đề xuất. Không sử dụng 'Thoát Sub' sau 'MsgBox' Không có thư mục như vậy." 'Làm sạch thích hợp :) Hãy nhớ rằng bạn vẫn còn Outlook đang chạy trong nền;) –