【问题标题】:search for emails with specific subject title IF UNREAD and save attachments into folder如果未读,则搜索具有特定主题标题的电子邮件并将附件保存到文件夹中
【发布时间】:2015-10-03 03:18:06
【问题描述】:

我正在使用以下 vba 代码,它应该搜索具有特定主题标题的所有电子邮件,即主题为“test”的电子邮件

只有当电子邮件未读时,才将该电子邮件的附件保存到文件夹中。

可能有一封或多封带有主题测试的电子邮件,所以我希望所有带有主题测试的未读电子邮件都将其附件保存到文件夹中。

这是我的代码:

Sub Work_with_Outlook()

 Set olApp = CreateObject("Outlook.Application")

     Dim olNs As Outlook.Namespace
     Dim Fldr As Outlook.MAPIFolder
     Dim myItem As Object
     Dim myAttachment As Outlook.Attachment
     Dim I As Long
     Dim olMail As Variant


     Set olApp = New Outlook.Application
     Set olNs = olApp.GetNamespace("MAPI")
     Set Fldr = olNs.GetDefaultFolder(olFolderInbox)
     Set myTasks = Fldr.Items

  Set UnRead = myTasks.Restrict("[UnRead] = False")
  Set olMail = myTasks.Find("[Subject] = ""test""")
  If Not (olMail Is Nothing) And UnRead.Count = 0 Then






    For Each myItem In myTasks
        If myItem.Attachments.Count <> 0 Then
            For Each myAttachment In myItem.Attachments
            If InStr(myAttachment.DisplayName, ".txt") Then
                I = I + 1
                myAttachment.SaveAsFile "\\uksh000-file06\Purchasing\NS\Unactioned\" & myAttachment
                End If
            Next
        End If

    Next


For Each myItem In myTasks
myItem.UnRead = False
Next

  MsgBox "Scan Complete."

  Else
  MsgBox "There Are No New Supplier Requests."
  End If

End Sub

这在某种程度上确实有效,如果我只有一封主题为“test”的电子邮件并且它未读,那么脚本将从该电子邮件中获取附件并将​​其保存到我的文件夹中。但是,如果我有一封主题为“test”的电子邮件已阅读,而另一封主题为“test”的电子邮件未读,那么代码将不起作用?

请有人告诉我哪里出错了?在此先感谢

【问题讨论】:

    标签: vba email outlook


    【解决方案1】:

    看起来您需要将两个条件组合成一个条件,并使用 Find/FindNext 或 Restrict 方法来获取 Items 类的实例,该实例包含与您的条件相对应的所有项目。例如:

     Set resultItems = myTasks.Restrict("[UnRead] = False AND [Subject] = ""test""")
    

    有关 MSDN 中的信息,请参阅 Enumerating, Searching, and Filtering Items in a Folder。

    您还可以在以下文章中找到示例代码:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-20
      • 2022-11-24
      • 1970-01-01
      • 2022-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多