【发布时间】:2018-09-20 17:07:46
【问题描述】:
我的代码根据附件名称对电子邮件进行排序。我需要 else 语句的帮助。
我希望将不符合参数的电子邮件移至主收件箱。
现在任何不符合参数的东西都只是移动到另一个文件夹。
正确的语法是什么?
Public WithEvents objMails As Outlook.Items
Private Sub Application_Startup()
Set objMails = Outlook.Application.Session.GetDefaultFolder (olFolderInbox).Items
End Sub
Private Sub objMails_ItemAdd(ByVal Item As Object)
Dim objMail As Outlook.MailItem
Dim objAttachments As Outlook.Attachments
Dim objAttachment As Outlook.Attachment
Dim strAttachmentName As String
Dim objInboxFolder As Outlook.Folder
Dim objTargetFolder As Outlook.Folder
"Ensure the incoming item is an email"
If TypeOf Item Is MailItem Then
Set objMail = Item
Set objAttachments = objMail.Attachments
"Check if the incoming email contains one or more attachments"
If objAttachments.Count > 0 Then
For Each objAttachment In objAttachments
strAttachmentName = objAttachment.DisplayName
Set objInboxFolder = Application.Session.GetDefaultFolder(olFolderInbox)
"Check the names of all the attachments"
"Specify the target folders"
If InStr(LCase(strAttachmentName), "some attachment name") > 0 Then
Set objTargetFolder = objInboxFolder.Folders("Target Folder")
Else: Set objTargetFolder = objInboxFolder.Folders("Target Folder 2")
End If
Next
Move the email to specific folder
objMail.Move objTargetFolder
End If
End If
Set objMail = Nothing
Set objAttachments = Nothing
Set objAttachment = Nothing
Set objInboxFolder = Nothing
Set objTargetFolder = Nothing
End Sub
【问题讨论】:
标签: vba if-statement outlook outlook-2013