比较两个outlook文件夹,并根据第二个文件夹中不存在的第一个文件夹过滤器电子邮件

时间:2017-09-09 10:38:03

标签: vba outlook-addin outlook-vba

我是vba的新手并且搜索过很多但是没有找到解决此问题的方法。我发现了几个重复的电子邮件过滤解决方案,但无论如何都无法解决我的问题。

如何比较两个outlook文件夹并根据第一个文件夹过滤掉根据{% extends "admin/change_list.html" %} {% load humanize admin_list%} {% block content_title %} <h1> Sales Summary </h1> {% endblock %} {% block result_list %} {% if action_form and actions_on_top and cl.show_admin_actions %}{% admin_actions %}{% endif %} {% if action_form and actions_on_bottom and cl.show_admin_actions %}{% admin_actions %}{% endif %} <div class="results"> <table> <thead> <tr> <th> <div class="text"> <a href="#">Action</a> </div> </th> <th> <div class="text"> <a href="#">Category</a> </div> </th> <th> <div class="text"> <a href="#">Total Sales</a> </div> </th> </tr> </thead> <tbody> {% for row in summary %} <tr class="{% cycle 'row1' 'row2' %}"> <td> {{ row.check_box }} </td> <td> {{ row.category__name }} </td> <td> {{ row.amount | intcomma }} </td> </tr> {% endfor %} </tbody> </table> </div> {% endblock %} {% block pagination %}{% endblock %} SentOn在第二个文件夹中不存在的电子邮件,并将这些邮件复制到第二个文件夹。 我的代码如下。

在这里,我可以使用简单的ReceiveTime获取两个文件夹中存在的邮件但是如果我将条件更改为If sMail.SentOn = dMail.SentOn那么它就无法正常工作。

If sMail.SentOn <> dMail.SentOn

调试输出Sub FindMails() Dim olApp As Outlook.Application Dim olNS As NameSpace Dim olFolder As Folder Dim olFolder2 As Folder Set olApp = Outlook.Application Set olNS = olApp.GetNamespace("MAPI") Set olFolder = olNS.Folders.Item("hayat_archive_Loc").Folders.Item("Inbox") Set olFolder2 = olNS.Folders.Item("Xhayat_archive_Loc").Folders.Item("Inbox") For Each sMail In olFolder.Items For Each dMail In olFolder2.Items If sMail.SentOn <> dMail.SentOn Then Debug.Print sMail.SentOn & vbTab & sMail.Subject End If Next Next End Sub

If sMail.SentOn = dMail.SentOn

调试输出9/9/2017 12:27:34 PM Access Problem 9/9/2017 9:07:33 AM Report on 08-Sep-2017. 9/9/2017 6:39:51 AM Handover of 08th September, 2017

If sMail.SentOn <> dMail.SentOn

1 个答案:

答案 0 :(得分:1)

部分我从这篇文章得到了这个问题的答案:

Check and Copy all emails from source folder those are not existed in destination folder

回答:Tim Williams

Sub CopyMail(SourceFolder As Outlook.Folder, DestinationFolder As Outlook.Folder)
Dim sMail As Object
Dim dMail As Object
Dim MailC As Object
Dim dictSent As New Scripting.dictionary, i As Long

'get a list of all unique sent times in the
'  destination folder
For Each dMail In DestinationFolder.Items
    dictSent(dMail.SentOn) = True
Next

'loop through the source folder and copy all items where
'  the sent time is not in the list
For i = SourceFolder.Items.Count To 1 Step -1
    Set sMail = SourceFolder.Items(i)

    If Not dictSent.Exists(sMail.SentOn) Then
        Set MailC = sMail.Copy        'copy and move
        MailC.Move DestinationFolder
        dictSent(sMail.SentOn) = True 'add to list
    End If

Next i

End Sub