2012-02-08 15 views
5

Bir outlook klasöründeki e-posta sayısını saymak için aşağıdaki kodu kullanın.Tarihe göre görünümlerde e-postaları sayma

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 

Ben her gün için bir sayım ile bitirmek böylece tarihe göre bu klasöre e-postaları saymaya çalışıyorum.

+0

Bu açıkça isnt [etiketi: vbscript] - VBA Outlook içinden demek? – brettdj

+0

Excel'e bağlanmak veya bir sorguyu çalıştırmak için ADO'yu kullanmak daha kolay olabilir: http://support.microsoft.com/kb/275262 – Fionnuala

cevap

10

Bu kodla deneyebilirsiniz:

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

Bu harika çalışmanın bir sorumluluğu, bu bilginin kaydedilebilmesi için herhangi bir yol var. bir csv dosyası? – Shaun07776

+2

@fmunkert: +1 Güzel bitti :) Sadece bir öneri. MsgBox "Böyle bir klasör yok" sonra 'Çıkış Sub' kullanmayın.' 'Düzgün bir temizlik yapın :) hala arka planda çalışan Outlook var;) –

İlgili konular