<

Never Again Forget to Add that Attachment to your Outlook Email

Published on
40,801 Points
27,701 Views
36 Endorsements
Last Modified:
Awarded
Editor's Choice
Community Pick
Issue.  Have you ever sent an email and forgotten to add the attachment?  How embarrassing!!!!  Here's a short Outlook macro to make sure this never happens again.

As I’ve gotten older I’ve noticed that my memory and attention to detail have both gotten worse.  One sign of this is the number of times I’ve sent a message that is supposed to include an attachment only to realize after I clicked "Send" that I forgot to add the attachment.  

If I catch the error, then I quickly send another message with the attachment and a note saying something like, "I guess it’d be handy if I included the attachment."  Sometimes I don’t notice that I forgot the attachment until I get a message from one of the recipients pointing out that I forgot to include it.  It’s not just me either.  I get several messages a month where the sender forgot to include an attachment.  

How do I know that a message should have included an attachment?  Because the message body typically mentions one.  Something like "the attached file", "the attachment", "the enclosed item", etc.  Wouldn’t it be great if Outlook could detect keywords like "attached", "attachment", "enclosed", or "enclosure" in the body of a message and then check to see if there are any attachments?  If there aren’t, then I’d like Outlook to warn me that I may have forgotten to add an attachment.  A feature like this would save me (and perhaps, you) a lot of embarrassment.

Background.  At least through the 2010 version Outlook does not have this ability.

Solution.  Once again Microsoft’s decision to make Outlook extensible via VBA (Visual Basic for Applications) offers us a simple solution.  This is one of the prime reasons I love Outlook.  With a few lines of VBA code you can make Outlook do almost anything you want it to.  In this case it takes about 30 lines of code to add the ability to check a message for keywords and take action based on the results.  

This solution works by trapping Outlook’s ItemSend event.  This event fires each time an item is sent.  The code then searches the body of the message for any of the keywords suggesting that the message should include an attachment.  If any of those keywords are found, then the code checks to see if there are any attachments.  If at least one attachment is found, then the message is sent.  However, if the code doesn’t find at least one attachment, then it displays a dialog-box warning you that you may have forgotten to add the attachment.  The dialog-box offers you the chance to cancel the send so you can add the missing attachment.

Requirements.  Outlook 2000 through 2010.  I've included two different versions, one for Outlook versions 2000 - 2003, and another for Outlook 2007 - 2010.  The reason for this is that Outlook 2007 and later includes features that make the code more useful.  

Instructions.

1. Add the Code to Outlook


Outlook 2000 - 2003.
   1.  Start Outlook.
   2.  Click ToolsMacroVisual Basic Editor.
   3.  If not already expanded, expand Microsoft Office Outlook Objects and click on ThisOutlookSession.
   4.  Copy the code below and paste it into the right-hand pane of Outlook's VB Editor window.
   5.  Edit the code as needed.  I included comment lines wherever something needs to or can change.
   6.  Click the diskette icon on the toolbar to save the changes.
   7.  Close the VB Editor.
   8.  Click ToolsMacroSecurity.
   9.  Set the "Security Level" to Medium.
  10. Close Outlook
  11. Start Outlook
  12. Outlook will display a dialog-box warning that ThisOutlookSession contains macros
        and asking if you want to allow them to run.  Click Yes.

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
    'On the next line edit the list of keywords as desired.  Be sure to separate each word with a | character.'
    Const KEYWORDS = "attached|attachment|attachments|enclosed|enclosure"
    'On the next line edit the message that will be displayed when the message should include an attachment as desired.'
    Const WARNING_MSG = "Wording in the message suggests that something is attached, but there are no items attached.  Do you want to cancel the send and add an attachment?"
    'On the next line edit the dialog-box title as desired.'
    Const MSG_TITLE = "Attachment Checker"
    Dim objRegEx As Object, colMatches As Object, bolAttachment As Boolean, olkAttachment As Outlook.Attachment
    Set objRegEx = CreateObject("VBscript.RegExp")
    With objRegEx
        .IgnoreCase = True
        .Pattern = KEYWORDS
        .Global = True
    End With
    Set colMatches = objRegEx.Execute(Item.Body)
    If colMatches.count > 0 Then
        For Each olkAttachment In Item.Attachments
            If olkAttachment.Type <> olEmbeddeditem Then
                bolAttachment = True
                Exit For
            End If
        Next
        If Not bolAttachment Then
            If msgbox(WARNING_MSG, vbQuestion + vbYesNo, MSG_TITLE) = vbYes Then
                Cancel = True
            End If
        End If
    End If
    Set olkAttachment = Nothing
    Set colMatches = Nothing
    Set objRegEx = Nothing
End Sub

Open in new window



Outlook 2007 - 2010.
   1.  Start Outlook.
   2.  Click ToolsMacroVisual Basic Editor.
   3.  If not already expanded, expand Microsoft Office Outlook Objects and click on ThisOutlookSession.
   4.  Copy the code below and paste it into the right-hand pane of Outlook's VB Editor window.
   5.  Edit the code as needed.  I included comment lines wherever something needs to or can change.
   6.  Click the diskette icon on the toolbar to save the changes.
   7.  Close the VB Editor.
   8.  Click ToolsTrust Center.
   9.  Click Macro Security.
  10. Set "Macro Security" to Warnings for all macros.
  11. Click OK.
  12. Close Outlook
  13. Start Outlook.  Outlook will display a dialog-box warning that ThisOutlookSession contains macros and asking if you want to allow them to run.  Click Yes.

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
    'On the next line edit the list of keywords as desired.  Be sure to separate each word with a | character.'
    Const KEYWORDS = "attached|attachment|attachments|enclosed|enclosure"
    'On the next line edit the message that will be displayed when the message should include an attachment as desired.'
    Const WARNING_MSG = "Wording in the message suggests that something is attached, but there are no items attached.  Do you want to cancel the send and add an attachment?"
    'On the next line edit the dialog-box title as desired.'
    Const MSG_TITLE = "Attachment Checker"
    Dim objRegEx As Object, colMatches As Object, bolAttachment As Boolean, olkAttachment As Outlook.Attachment
    Set objRegEx = CreateObject("VBscript.RegExp")
    With objRegEx
        .IgnoreCase = True
        .Pattern = KEYWORDS
        .Global = True
    End With
    Set colMatches = objRegEx.Execute(Item.Body)
    If colMatches.count > 0 Then
        For Each olkAttachment In Item.Attachments
            If Not IsEmbedded(olkAttachment) Then
                bolAttachment = True
                Exit For
            End If
        Next
        If Not bolAttachment Then
            If msgbox(WARNING_MSG, vbQuestion + vbYesNo, MSG_TITLE) = vbYes Then
                Cancel = True
            End If
        End If
    End If
    Set olkAttachment = Nothing
    Set colMatches = Nothing
    Set objRegEx = Nothing
End Sub

Function IsEmbedded(olkAttachment As Outlook.Attachment) As Boolean
    'Purpose: Determines if an attachment is embedded.'
    'Written: 9/14/2009'
    'Author: BlueDevilFan'
    'Outlook: 2007'
    Dim olkPA As Outlook.PropertyAccessor
    Set olkPA = olkAttachment.PropertyAccessor
    On Error Resume Next
    IsEmbedded = (olkPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E") <> "")
    On Error GoTo 0
    Set olkPA = Nothing
End Function

Open in new window


2. Test the Solution


Create a test message.  The message should include one of the keywords.  Make sure that you don’t add an attachment.  Click Send.  Outlook should display a pop-up message that looks something like this.  If you click Yes, then Outlook will abort the send giving you the opportunity to add the missing attachment.  Clicking No sends the message on its way immediately.
Outlook Attachment Checker Warning Message
Enhancements.  You can expand the list of keywords that suggests the item should include an attachment by editing line #3 of the code.  Right now the code searches for the words "attached", "attachment", "attachments", "enclosed", or "enclosure".  For example, say that you routinely email .zip files with a message like "Your report is in the ZIP file."  Adding the word "ZIP" to the list of keywords would help ensure that you don't send one of these messages without the required file.
36
Author:David Lee
Ask questions about what you read
If you have a question about something within an article, you can receive help directly from the article author. Experts Exchange article authors are available to answer questions and further the discussion.
Get 7 days free