@TechnicLee in outlook 2013 can you code to remove the attachments as you send them, so your sent items folder has emails w/o attachments
— Rick Honeycutt (@rick_honeycutt) December 1, 2016
I received that tweet a few days ago. Here’s the solution I put together for Rick.
Requirements.
This solution should work in all versions of Outlook from 2003 on.
Instructions.
Follow these instructions to add the code to Outlook.
- If not already expanded, expand Microsoft Office Outlook Objects and click on ThisOutlookSession
- Copy the code from the code snippet box and paste it into the right-hand pane of Outlook’s VB Editor window
- Click the diskette icon on the toolbar to save the changes
- Close the VB Editor
- Click File and select Options
- When the Outlook Options dialog appears click Trust Center then click the Trust Center Settings button
- Click Macro Settings
- Select either of the two bottom settings (i.e. Notifications for all macros or Enable all macros (not recommended; potentially dangerous code can run). The choice of which to choose is up to you. If you select Notifications, then you’ll be prompted at times to enable macros. If you pick Enable all then there’s a chance that a malicious macro could run. It’s a question of how much risk you want to assume.
- Click Ok until the dialog-boxes have all closed
- Close Outlook
- Start Outlook. Outlook will display a dialog-box warning that ThisOutlookSession contains macros and asking if you want to allow them to run. Say yes.
Dim WithEvents olkSnt As Outlook.Items
Private Sub Application_Quit()
Set objSnt = Nothing
End Sub
Private Sub Application_Startup()
Set objSnt = Session.GetDefaultFolder(olFolderSentMail)
End Sub
Private Sub olkSnt_ItemAdd(ByVal Item As Object)
Dim olkAtt As Outlook.Attachment, lngCnt As Long
For lngCnt = Item.Attachments.count To 1 Step -1
Set olkAtt = Item.Attachments(lngCnt)
If Not IsHiddenAttachment(olkAtt) Then
olkAtt.Delete
End If
Next
Item.Save
Set olkAtt = Nothing
End Sub
Private Function IsHiddenAttachment(olkAtt As Outlook.Attachment) As Boolean
Const PR_ATTACH_CONTENT_ID = "http://schemas.microsoft.com/mapi/proptag/0x3712001E"
Dim olkPA As Outlook.PropertyAccessor, varTemp As Variant
On Error Resume Next
Set olkPA = olkAtt.PropertyAccessor
varTemp = olkPA.GetProperty(PR_ATTACH_CONTENT_ID)
IsHiddenAttachment = (varTemp <> "")
On Error GoTo 0
Set olkPA = Nothing
End Function