Search code examples
vbaoutlookemail-attachmentsoutlook-2010

Exclude Signature images when Exporting attachments to a folder


I have code to export Outlook attachments to a local folder.

I noticed the little images in the signature are also saved as attachments.

I think excluding the signature images could be done with an If then along the lines of:

 For i = lngCount To 1 Step -1
 If objAttachments.Item(i).Size > 6000 Then

I don't know where to place in my code, especially the End If (after or before the Next).

Here is my code:

Public Sub Renamefileandexcludesignature(Item As Outlook.MailItem)
    Dim Atmt As Outlook.Attachment
    Dim SavePath As String
    Dim FileName As String
    SavePath = "C:\Users\Antoine\Documents"
    FileName = "Antoine" & ".csv"
    For Each Atmt In Item.Attachments
         Atmt.SaveAsFile SavePath & "\" & FileName
    Next
    Set Atmt = Nothing
End Sub

Solution

  • If you're after downloading a specific file type you could check the extension of the attachment (untested, but should work):

    Public Sub Renamefileandexcludesignature(Item As Outlook.MailItem)
        Dim Atmt As Outlook.Attachment
        Dim SavePath As String
        Dim FileName As String
    
        Dim objFSO As Object
        Dim sExt As String
    
        SavePath = "C:\Users\Antoine\Documents"
        FileName = "Antoine" & ".csv"
    
        Set objFSO = CreateObject("Scripting.FileSystemObject")
    
        For Each Atmt In Item.Attachments
            sExt = objFSO.GetExtensionName(Atmt.FileName)
    
            Select Case sExt
                Case "jpg", "png"
                    'Do nothing
                Case Else
                    Atmt.SaveAsFile SavePath & "\" & FileName
            End Select
        Next
    
        Set Atmt = Nothing
    End Sub
    

    But I have little to no knowledge in VBA and don't know where to place in my code, especially the EndIf ( after or before the Next)

    These have to go in order -
    If you use IF and then FOR you have to use NEXT first to close the FOR and then END IF to close the IF.
    If you use FOR and then IF you have to use END IF to close the IF before you can use NEXT to close the FOR.

    Hope that made sense.