Need help removing some attachments from email

logandiana

Board Regular
Joined
Feb 21, 2017
Messages
107
I have some rules in Outlook that forward emails with attachments.
The problem is that the Outlook rule can't seem to figure out the difference between an actual attachment, like an xlsx, pdf, docx, etc., versus something like a small png picture that is embedded in someone's signature panel.
I need the rule to just forward the emails that have that paperclip attachment icon in Outlook. On the receiving end they are getting many emails without any documents attached, but come to find out that there is some small .jpg or png somewhere in the email.

First off is there anyway to specify this in the rules? to only forward those with 'actual' attachments? I didn't find any.

So I though maybe I could use Outlook VBA to remove any and all attachments that have a certain criteria.
I sent an email to myself packed with several different types of attachments to see if I could forward the email, but only include xlsx attachments, but the code keeps removing everything.
Can someone look at it? I feel like I am close.

VBA Code:
Sub ChangeSubjectForwardtestscript(myItem As Outlook.MailItem)

Dim myInspector As Outlook.Inspector
Dim myAttachments As Outlook.Attachments
Dim LA As Long
Dim i As Integer

On Error Resume Next
If myItem.UnRead = True Then

    Set myInspector = Application.ActiveInspector
    Set myItem = myInspector.CurrentItem.Forward
    Set myAttachments = myItem.Attachments
    
LA = myAttachments.Count

For i = LA To 1 Step -1

    If Right(myAttachments.myItem(i).DisplayName, 4) <> "xlsx" Then
        myAttachments.Remove i
    End If

Next i

LA = myAttachments.Count

myItem.Recipients.Add "email@email.com"
myItem.Send
myItem.UnRead = False

End If
End Sub
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
See if you can incorporate the code here:


To make it work with your code I think you need to change every occurrence of outMailItemto myItem.

I just tested it and the only change I made (for Outlook 2021) is this part of the IsFileAttachment function which checks the ATTACH_CONTENT_ID property:
VBA Code:
    If IsFileAttachment Then
        Set outPropertyAccessor = outAttachment.PropertyAccessor
        propertyValue = outPropertyAccessor.GetProperty(PR_ATTACH_CONTENT_ID)
        Debug.Print "ATTACH_CONTENT_ID = "; propertyValue
        If propertyValue <> "" Then
            'The existence of a PR_ATTACH_CONTENT_ID property value doesn't necessarily mean that the attachment is not a file attachment.
            'For example, Gmail assigns a PR_ATTACH_CONTENT_ID property to each file attachment
            'If the ContentID occurs in a HTML img src attribute it means the attachment is embedded in the email body and therefore not a file attachment
            If InStr(1, allImgSrc, "cid:" & propertyValue, vbTextCompare) Then IsFileAttachment = False
        End If
    End If
In addition, as discussed in that thread, the ATTACH_MIME_TAG now returns a sensible value, however ATTACH_FLAGS is still zero and ATTACH_CONTENT_LOCATION is an empty string:
Code:
ATTACH_MIME_TAG = image/png
ATTACH_FLAGS =  0 
ATTACH_CONTENT_LOCATION =
so I guess the checks for the latter 2 properties could be removed.
 
Upvote 0
Solution

Forum statistics

Threads
1,214,973
Messages
6,122,534
Members
449,088
Latest member
RandomExceller01

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top