【问题标题】:Copy emails from source folder if not existing in destination folder如果目标文件夹中不存在,则从源文件夹复制电子邮件
【发布时间】:2018-02-18 00:00:21
【问题描述】:

我正在使用 Visual Studio 构建一个插件来复制电子邮件。

条件是检查,根据SentOn/ReceivedTime,只复制源文件夹中不存在于目标文件夹的邮件。

我尝试了下面的代码,但它给了我一个错误System.OutOfMemoryException Out of memory or system resources

Sub CopyMail(SourceFolder As Outlook.Folder, DestinationFolder As Outlook.Folder)
    Dim sMail As Object
    Dim dMail As Object
    Dim MailC As Object
    For Each sMail In SourceFolder.Items
        For Each dMail In DestinationFolder.Items
            If sMail.SentOn <> dMail.SentOn Then
                MailC = sMail.Copy
                MailC.Move(DestinationFolder)
            End If
        Next
    Next
End Sub

【问题讨论】:

  • 什么是 dFolder ?
  • 单步执行代码时会发生什么? .... 使用 F8 键
  • @0m3r 根据SentOn时间比较
  • @jsotola Outlook 在运行 F8/调试模式后卡住,(实际上我正在尝试这个 vba 来构建一个 Outlook 加载项)
  • stuck 是什么意思? ....你的意思是它冻结了吗? .....哪一行让它冻结了?

标签: vba visual-studio vsto outlook-addin


【解决方案1】:

嵌套循环中存在逻辑错误 - 对于目标文件夹中的每个项目,您从源文件夹中复制所有不匹配项,即使这些项目可能与目标文件夹中的其他项目匹配。

这是一种应该可行的方法(未经测试)。 它在 VBA 中:我的 VB.NET 不好,无论如何你用 VBA 标记...

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

【讨论】:

  • 非常感谢,我已经测试了您的代码,它主要解决了我的目的。在我阅读所有这些 cmets 和答案时,我认为这足以通过仅过滤 SentOn 来确定邮件是唯一的。 **考虑到有时我们会在同一时间(秒)收到几封主题相同或不同的电子邮件,我将如何确定一封邮件是否唯一**
猜你喜欢
  • 1970-01-01
  • 2017-03-31
  • 1970-01-01
  • 2021-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-29
  • 2023-01-11
相关资源
最近更新 更多