【发布时间】: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”的电子邮件未读,那么代码将不起作用?
请有人告诉我哪里出错了?在此先感谢
【问题讨论】: