2

在“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
4

1 回答 1

4

您可以使用If-Then-Else 语句Case 语句在 VBA 代码中执行条件分支。下面提供了来自前面的 MSDN 链接的示例。

If-The-Else 语句

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 代码将永远不会被执行。

于 2012-11-14T14:23:06.273 回答