VBA Microsoft Outlook邮件处理 - 添加另一个案例

时间:2012-11-14 10:34:18

标签: vba outlook outlook-vba

在'ThisOutlookSession'中,我有这个子抓取特定的附件。如何添加另一个条件,以便选择另一个特定邮件和附件?

Private Sub Items_ItemAdd(ByVal item As Object)

    On Error GoTo ErrorHandler

    'Only act if it's a MailItem
    Dim Msg As Outlook.MailItem
    If TypeName(item) = "MailItem" Then
        Set Msg = item

        'Change variables to match need. Comment or delete any part unnecessary.
        If (Msg.SenderName = "Sender") And _
            (Msg.Subject = "Sub") And _
            (Msg.Attachments.Count >= 1) Then

            'Set folder to save in.
            Dim olDestFldr As Outlook.MAPIFolder
            Dim myAttachments As Outlook.Attachments
            Dim Att As String

            'location to save in.  Can be root drive or mapped network drive.
            Const attPath As String = "Z:\Folder\Folder\"

            ' save attachment
            Set myAttachments = item.Attachments
            Att = myAttachments.item(1).DisplayName
            myAttachments.item(1).SaveAsFile attPath & Att

            ' mark as read
            Msg.UnRead = False
        End If

    End If

ProgramExit:
    Exit Sub

ErrorHandler:
    MsgBox Err.Number & " - " & Err.Description
    Resume ProgramExit
End Sub

1 个答案:

答案 0 :(得分:4)

您可以使用If-Then-Else statementsCase statements在VBA代码中执行条件分支。以下MSDN链接提供了样本。

If-Then-Else Statement

If dayW = DayOfWeek.Wednesday Then 
    If hour = 14 Or hour = 15 Then 
        Return True 
    Else 
        Return False 
    End If 
ElseIf dayW = DayOfWeek.Thursday Then 
    If hour = 12 Then 
        Return True 
    Else 
        Return False 
    End If 
Else 
    Return False 
End If 

案例陈述

Select Case number
    Case 1 To 5
        Debug.WriteLine("Between 1 and 5, inclusive")
        ' The following is the only Case clause that evaluates to True. Case 6, 7, 8
        Debug.WriteLine("Between 6 and 8, inclusive")
    Case 9 To 10
        Debug.WriteLine("Equal to 9 or 10")
    Case Else
        Debug.WriteLine("Not between 1 and 10, inclusive")
End Select

如果您的计算机未启用,那么您的VBA代码将永远不会被执行。