blog.onlysimpler.com

A blog... only simpler.

Attachment Check - 07 March 2006

How many times have you done this...

  1. Compose email
  2. Click 'Send'
  3. Swear profusely
  4. Add the attachment and resend it

I never learn. I've accepted that I'm destined to do this for the rest of life. But never fear, I have the answer. Well, assuming you use Outlook at least.

This snippet of VBA code should do the trick nicely.

'
' Don't you hate it when you send an email to someone and forget to attach
' the file you're sending! Well, this function checks the body and subject of
' your message for the character sequence "attach". If it finds it, but
' there's no attachment, then it pops up a window asking if you're sure you
' want to send the message. It gives you a second chance to attach the file
' saving you the embarrassment of sending a phantom attachment :)
'
' The program is smart enough to only search the text that you have written.
' ie: it wont search your message below the "-----Original Message-----"
' marker. Unfortunately, it may still pick up the occasional case where you
' are replying to someone else and they have included the word "attach" in
' the subject line. Hopefully, it wont cause too much grief, and the cases
' when it does save you will be worth the trouble.
'
' Shane Bell, Feb 2006
'
' adapted from a script by Dan Evans - http://www.danevans.co.uk/vba/
'

Private Sub Application_ItemSend(ByVal item As Object, Cancel As Boolean)

' variables
Dim msgText As String
Dim oldMessageStart As Integer
Dim popupText As String
Dim result As Integer

' find the start of any old messages (we don't want to check their contents)
oldMessageStart = InStr(item.Body, "-----Original Message-----")

If oldMessageStart = 0 Then
  ' index is 0, so this must be a new message, get all the text in the message
  msgText = item.Body + " " + item.Subject
Else
  ' message is a reply or a forward, only get the text from the new part of the message
  msgText = Left(item.Body, oldMessageStart) + " " + item.Subject
End If

' does the message body or subject contain the characters "attach" anywhere?
If InStr(LCase(msgText), "attach") Then

  ' and does the message NOT have any attachments?
  If item.Attachments.Count = 0 Then

    'display a warning to the user, and ask them to confirm

    ' construct the warning message
    popupText = "The text of your message seems to indicate that you are supposed to attach something."
    popupText = popupText & Chr(13) & Chr(10)
      popupText = popupText & Chr(13) & Chr(10)
      popupText = popupText & "Do you want to send it anyway?"

      ' get the response from the user (ok, or cancel)
      result = MsgBox(popupText, vbYesNo + vbDefaultButton2 + vbExclamation, "Did you forget the attachment?")
      If result = vbNo Then
        Cancel = True
      End If
    End If
  End If

End Sub

I can't take credit for the code, thank Dan Evans for it. I just tweaked a few things and cleaned it up a little. Detailed instructions on how to install it into Outlook are posted on Dan's site.

Enjoy.

link