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-Then-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 に答える