locked
Count specific attachments in outlook RRS feed

  • Question

  • I am not well experienced person in VBA

    I need to write a VBA code to count attachments in selcted email.  I know how to do it for all files in an email. The point is I need to do it only for csv files.

    Exmaple:

    Email attachments: test.pdf, test.xslx, test.csv

    Outcome (MsgBox or better excel: 1 csv file found in the email.

    Please find my code that needs to be enhanced 

    Sub XYZcountLK()
    
    Dim oItem As Object
    Dim iAtt As Integer
    
    'Set oItem = oItem & "\*.*"
    
    For Each oItem In ActiveExplorer.Selection
    iAtt = oItem.Attachments.Count + iAtt
    Next
    
    MsgBox "Selected " & ActiveExplorer.Selection.Count & "message with " & iAtt & "attachements"
    
    End Sub

    Can you please help me to solve this issue

    BR

    Friday, July 12, 2019 12:10 PM

All replies

  • Like this:

    Sub XYZcountLK()
        Dim oItem As Object
        Dim oAttachment As Attachment
        Dim iAtt As Integer
        For Each oItem In ActiveExplorer.Selection
            For Each oAttachment In oItem.Attachments
                If LCase(Right(oAttachment.FileName, 4)) = ".csv" Then
                    iAtt = oItem.Attachments.Count + iAtt
                End If
            Next oAttachment
        Next oItem
        MsgBox "Selected " & ActiveExplorer.Selection.Count & " message(s) with " & _
            iAtt & " .csv attachement(s)"
    End Sub
    


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Friday, July 12, 2019 12:22 PM
  • Hi Hans,
    Thank you very much for help
    Unfortunately your code is given me the same results. I sent to myself an email with two attachments (csv and png)
    The results is still 2 instead of 1

    BR

    Friday, July 12, 2019 2:28 PM
  • Hi,

    Got it thank you for help once again

    Sub XYZcountLK_1()
        Dim oItem As Object
        Dim oAttachment As attachment
        Dim iAtt As Integer
        For Each oItem In ActiveExplorer.Selection
            For Each oAttachment In oItem.Attachments
                If LCase(Right(oAttachment.FileName, 4)) = ".csv" Then
                    iAtt = iAtt + 1
                End If
            Next oAttachment
        Next oItem
        MsgBox "Selected " & ActiveExplorer.Selection.Count & " message(s) with " & _
            iAtt & " .csv attachement(s)"
    End Sub

    Friday, July 12, 2019 3:09 PM
  • Sorry about my mistake, I'm glad you were able to solve it yourself.

    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Friday, July 12, 2019 3:34 PM