【发布时间】:2019-07-25 13:56:16
【问题描述】:
我为 Outlook 编写了一个脚本来移动电子邮件,具体取决于附件。
脚本是检查所有到达电子邮件的规则的一部分。
如果邮件没有附件,或者附件是不同于 .pdf 的文件(.zip、.jpg 或其他),它会转到错误文件夹。
如果电子邮件附有 .pdf 文件,它会保留在收件箱中。
一切正常,除非邮件有两个附件 - 一个 .pdf 文件和一个 .zip 文件。
- 如果 .zip 文件是第一个附件,邮件会按原样转到错误文件夹。
- 如果 .pdf 文件是第一个附件,它会保留在收件箱中,但它也应该转到错误文件夹。
我尝试修改循环和 if,但结果始终相同。我尝试使用“Continue For”或“Continue Loop”。
Sub PDF(Item As Outlook.MailItem)
Dim myAtt As Outlook.Attachment
For Each myAtt In Item.Attachments
If Not Right(LCase(myAtt.FileName), 4) = ".pdf" Then
Item.Move Session.GetDefaultFolder(olFolderInbox).Parent.Folders("Error")
End If
Exit For
Next
Set myAtt = Nothing
End Sub
我希望将带有至少一个不同于 .pdf 的附件的电子邮件发送到错误文件夹。只有带有 .pdf 附件的电子邮件才能保留在收件箱中。
如果其中至少有一个不是 .pdf,有没有办法移动邮件?我不明白为什么它只检查第一个附件。
解决方案
Sub PDF2(Item As Outlook.MailItem)
Dim myAtt As Outlook.Attachment
Dim allPdf As Boolean
allPdf = True
For Each myAtt In Item.Attachments
Debug.Print myAtt.DisplayName
If Right(LCase(myAtt.FileName), 4) <> ".pdf" Then
allPdf = False
Exit For
End If
Next
If allPdf = False Or Item.Attachments.Count = 0 Then
Item.Move Session.GetDefaultFolder(olFolderInbox).Parent.Folders("Error")
End If
Set myAtt = Nothing
End Sub
仍然需要在隐藏附件和普通附件之间添加识别,但此脚本还将没有任何附件的邮件移动到错误文件夹。
【问题讨论】: