简体   繁体   English

按发件人计数 Email outlook VBA

[英]Count Email by sender outlook VBA

i have to do a macro for count email by sender我必须为发件人计数 email 做一个宏

this is my code这是我的代码

    Dim objDictionary As Object
    Dim objInbox As Outlook.Folder
    Dim i As Long
    Dim objMail As Outlook.MailItem
    Dim strSender As String
    Dim objExcelApp As Excel.Application
    Dim objExcelWorkbook As Excel.Workbook
    Dim objExcelWorksheet As Excel.Worksheet
    Dim varSenders As Variant
    Dim varItemCounts As Variant
    Dim nLastRow As Integer
 
    Set objDictionary = CreateObject("Scripting.Dictionary")
    Set objInbox = Outlook.Application.Session.GetDefaultFolder(olFolderInbox)
 
    For i = objInbox.Items.Count To 1 Step -1
        If objInbox.Items(i).Class = olMail Then
           Set objMail = objInbox.Items(i)
           strSender = objMail.SenderEmailAddress
 
           If objDictionary.Exists(strSender) Then
              objDictionary.Item(strSender) = objDictionary.Item(strSender) + 1
           Else
              objDictionary.Add strSender, 1
           End If
        End If
    Next

    Set objExcelApp = CreateObject("Excel.Application")
    objExcelApp.Visible = True
    Set objExcelWorkbook = objExcelApp.Workbooks.Add
    Set objExcelWorksheet = objExcelWorkbook.Sheets(1)
 
    With objExcelWorksheet
         .Cells(1, 1) = "Sender"
         .Cells(1, 2) = "Count"
    End With
 
    varSenders = objDictionary.Keys
    varItemCounts = objDictionary.Items
 
    For i = LBound(varSenders) To UBound(varSenders)
        nLastRow = objExcelWorksheet.Range("A" & objExcelWorksheet.Rows.Count).End(xlUp).Row + 1
        With objExcelWorksheet
             .Cells(nLastRow, 1) = varSenders(i)
             .Cells(nLastRow, 2) = varItemCounts(i)
        End With
    Next
 
    objExcelWorksheet.Columns("A:B").AutoFit
End Sub

The problem is when i execute the code, i have this problem enter image description here问题是当我执行代码时,我有这个问题在此处输入图像描述

the error say: the underlying security system cannot find your digital ID错误说:底层安全系统找不到您的数字身份证

can you help me pls?你能帮我吗?

First of all, there is no need to iterate over all items in the folder:首先,不需要遍历文件夹中的所有项目:

 For i = objInbox.Items.Count To 1 Step -1
        If objInbox.Items(i).Class = olMail Then

Instead, you need to use the Find / FindNext or Restrict methods of the Items class to get only items that correspond to your conditions.相反,您需要使用Items class 的Find / FindNextRestrict方法来仅获取与您的条件相对应的项目。 Read more about these methods in the articles I wrote:在我写的文章中阅读更多关于这些方法的信息:

For example, you could use the following search criteria to get items from a specific sender's email address:例如,您可以使用以下搜索条件从特定发件人的 email 地址获取项目:

criteria = "@SQL=" & Chr(34) _ 
& "urn:schemas:httpmail:senderemail" & Chr(34) _ 
& " = 'some@email.com'"

or better yet:或者更好:

 Filter = "@SQL=" & " urn:schemas:httpmail:senderemail Like '%some@email.com%'"

See Filtering Items Using a String Comparison for more information.有关详细信息,请参阅使用字符串比较过滤项目

After you have applied a filter to the collection you may check out the Count property of the Items class which can give you the number of items from a specific sender.对集合应用过滤器后,您可以查看Items class 的Count属性,它可以为您提供来自特定发件人的项目数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM