Consulting

Results 1 to 2 of 2

Thread: Save attachment but excluding all jpg and png file

  1. #1

    Save attachment but excluding all jpg and png file

    Hi all

    I tried looking on the post already but seem couldnt have an solution.


    I would like to add something or below to the code which work at current. I would like to exclude all jpg and png file type.
    'want to delete image file
    If UCase(Right(objAtt.FileName, 3)) <> "PNG" And UCase(Right(objAtt.FileName, 3)) <> "GIF" Then
    strFile = strPath & objAtt.FileName
    objAtt.SaveAsFile strFile
    objTargetItem.Attachments.Add strFile, , , objAtt.DisplayName
    fso.DeleteFile strFile
    End If



    Im currently having this code work upon receiving each email and autosave the attachment.


    Public Sub Test(itm As Outlook.MailItem)
    Dim objAtt As Outlook.Attachment
    Dim saveFolder As String


    From = itm.Sendername


    saveFolder = "C:\Users\abcbac\Documents\Emailattachment"
    For Each objAtt In itm.Attachments


    stFileName = saveFolder & "" & Format$(itm.CreationTime, "yyyy.mm.dd hhmm - ") & From & " - " & objAtt.DisplayName
    i = 1
    JumpHere:
    If Dir(stFileName) = "" Then
    objAtt.SaveAsFile stFileName
    Else
    i = i + 1
    stFileName = saveFolder & "" & Format$(itm.CreationTime, "yyyy.mm.dd hhmm - ") & From & (" - " & i & " - ") & objAtt.DisplayName
    GoTo JumpHere
    End If


    Set objAtt = Nothing
    Next

  2. #2
    Your proposed code saves the attachments then attempts to delete the saved attachments. I am not sure what the point of that would be?

    I suspect what you are looking for is

    Public Sub Test(itm As Outlook.MailItem)
    Dim objAtt As Outlook.Attachment
    Dim saveFolder As String
    Dim stFilename As String
    Dim From As String
    Dim i As Integer
    
        From = itm.SenderName
    
        saveFolder = "C:\Users\abcbac\Documents\Emailattachment"
        For Each objAtt In itm.Attachments
            If UCase(Right(objAtt.fileName, 3)) <> "PNG" And UCase(Right(objAtt.fileName, 3)) <> "JPG" Then
                stFilename = saveFolder & "" & Format$(itm.CreationTime, "yyyy.mm.dd hhmm - ") & From & " - " & objAtt.fileName
                i = 1
    JumpHere:
                If Dir(stFilename) = "" Then
                    objAtt.SaveAsFile stFilename
                Else
                    i = i + 1
                    stFilename = saveFolder & "" & Format$(itm.CreationTime, "yyyy.mm.dd hhmm - ") & From & (" - " & i & " - ") & objAtt.fileName
                    GoTo JumpHere
                End If
            End If
            Set objAtt = Nothing
        Next
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •