Delete All Attachments on Sent Items


 

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.

  1. If not already expanded, expand Microsoft Office Outlook Objects and click on ThisOutlookSession
  2. Copy the code from the code snippet box and paste it into the right-hand pane of Outlook’s VB Editor window
  3. Click the diskette icon on the toolbar to save the changes
  4. Close the VB Editor
  5. Click File and select Options
  6. When the Outlook Options dialog appears click Trust Center then click the Trust Center Settings button
  7. Click Macro Settings
  8. 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.
  9. Click Ok until the dialog-boxes have all closed
  10. Close Outlook
  11. 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
Advertisement