vbaoutlookemail-attachments

Delete redundant images when replying to an email with the original attachments


When replying to an email message, the original attachments are not included on the reply message.

The below code works, except it sometimes adds extra redundant images to the reply message.

I found that these images have the same name pattern, image & number & .png or Jpg ,like image001.png , image002.png , image003.Jpg , and so on.
enter image description here

These extra images are pictures of the signatures of other persons of the original email.

I need to amend the code to delete these extra redundant images.

Sub ReplyAllWithAttachments()
    ReplyAndAttach (True)
End Sub

Function GetCurrentItem() As Object
    Dim objApp As Outlook.Application
 
    Set objApp = Application
    On Error Resume Next
    Select Case TypeName(objApp.ActiveWindow)
        Case "Explorer"
            Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1)
        Case "Inspector"
            Set GetCurrentItem = objApp.ActiveInspector.CurrentItem
    End Select
 
    Set objApp = Nothing
End Function
 
Public Sub AddOriginalAttachments(ByVal MyItem As Object, ByVal myResponse As Object)
    Dim MyAttachments As Variant
    Set MyAttachments = myResponse.Attachments
 
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fldTemp = fso.GetSpecialFolder(2)  'User Temp Folder
    strPath = fldTemp.Path & "\"
 
    For Each Attachment In MyItem.Attachments
        strFile = strPath & Attachment.FileName
        Attachment.SaveAsFile strFile
        MyAttachments.Add strFile, , , Attachment.DisplayName
        fso.DeleteFile strFile
    Next
 
    Set fldTemp = Nothing
    Set fso = Nothing
    Set MyAttachments = Nothing
End Sub
 
Public Sub ReplyAndAttach(ByVal ReplyAll As Boolean)
    Dim MyItem As Outlook.MailItem
    Dim oReply As Outlook.MailItem
 
    Set MyItem = GetCurrentItem()
    If Not MyItem Is Nothing Then
 
        If ReplyAll = False Then
            Set oReply = MyItem.Reply
        Else
            Set oReply = MyItem.ReplyAll
        End If
 
        AddOriginalAttachments MyItem, oReply
        oReply.Display
        MyItem.UnRead = False
    End If
 
    Set oReply = Nothing
    Set MyItem = Nothing
End Sub

Solution

  • Please, try replacing this code part:

       For Each Attachment In MyItem.Attachments
            strFile = strPath & Attachment.FileName
            Attachment.SaveAsFile strFile
            MyAttachments.Add strFile, , , Attachment.DisplayName
            fso.DeleteFile strFile
        Next
    

    with this slightly modified one:

       For Each Attachment In MyItem.Attachments
         If Not Attachment.FileName Like "*image###.png" And _
               Not Attachment.FileName Like "*image###.jpg" Then
            strFile = strPath & Attachment.FileName
            Attachment.SaveAsFile strFile
            MyAttachments.Add strFile, , , Attachment.DisplayName
            fso.DeleteFile strFile
          End If
       Next
    

    Not tested, of course, but I think it should solve the problem. Not allowing to existing attachment named as that specific pattern to be processed in the existing way (save and reattach to the replay message).