May 042010
 

I hate it when I send an email only to get an email back from the recipient saying that I forgot to include the attachment that I referred to in my email.  How annoying.  I also hate it when I forget to fill in the subject line.  I don’t like receiving emails with no subject lines and I hate it even more when I accidentally send one myself.  So I found some VBA macros that I could add to Outlook which will look for certain criteria which may indicate a forgotten attachment.  The missing subject line is a little more straightforward. I always want a subject line and when I hit the send button, it’s either there or it isn’t, but the attachment detector has to first figure out if I intended to attach something.  So, let’s just jump into the code.  I found the majority of this code at http://msmvps.com/blogs/athif/pages/48968.aspx but you will see that I made some changes to it to suit my needs.


Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim lngres As Long
If InStr(1, Item.Body, "attach") Or InStr(1, Item.Subject, "attach") <> 0 Then
If Item.Attachments.Count = 0 Then
lngres = MsgBox("word 'Attach' found, but no attachment - send anyway?", _
vbYesNo + vbDefaultButton2 + vbQuestion + vbSystemModal, "You asked me to warn you...")
If lngres = vbNo Then Cancel = True
End If
End If

If InStr(1, Item.Body, "include") Or InStr(1, Item.Subject, "include") <> 0 Then
If Item.Attachments.Count = 0 Then
lngres = MsgBox("word 'include' found, but no attachment - send anyway?", _
vbYesNo + vbDefaultButton2 + vbQuestion + vbSystemModal, "You asked me to warn you...")
If lngres = vbNo Then Cancel = True
End If
End If

If InStr(1, Item.Body, "enclose") Or InStr(1, Item.Subject, "enclose") <> 0 Then
If Item.Attachments.Count = 0 Then
lngres = MsgBox("word 'Enclose' found, but no file enclosed - send anyway?", _
vbYesNo + vbDefaultButton2 + vbQuestion + vbSystemModal, "You asked me to warn you...")
If lngres = vbNo Then Cancel = True
End If
End If

strSubject = Item.Subject
If Len(Trim(strSubject)) = 0 Then
Prompt$ = "Subject is Empty. Are you sure you want to send the Mail?"
If MsgBox(Prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check for Subject") = vbNo Then
Cancel = True
End If
End If
End Sub

The first thing to notice is that this macro is executed whenever you click the send button (or press ctrl-enter). Whenever you start the send process, this macro executes. The first three parts of the code look for the words “Attach”, “Include” and “Enclose” in the body of the email as well as the subject line. Sometimes I just send an email with the subject line “Final report attached” and nothing in the body. I want the trigger to recognize this. Also, the triggers are not case sensitive and they will also hit on other forms of the word, such as “Attached”. So, if one of those key words is found, it will then check to see if there are any attachments in the email. If there are no attachments, a dialog box is presented stating that one of the keywords was spotted yet there were no attachments found. Do you still want to send the email? It can happen that you might be replying to a quoted email that someone sent you and had the word “attach” in it. Most people when they reply to an email, they include the original sender’s quoted email. You may not have any intention of including an attachment in this case, but the macro will still find the word “attach”, so it gets triggered. I wish there was a way to have the macro only look at the new text that you added, but there doesn’t seem to be. If you answer “No” to the question “Do you still want to send”, then the send process is cancelled and you will have a chance to add your attachment. If you choose “Yes” then the email is sent as usual.

The last section of code looks for blank subject lines. The methodology is the same as the other sections, and is probably self explanatory.

To make this code work in your Outlook, just copy the code to the clipboard, open Outlook and press alt-F11. On the left side, you will see a standard navigation tree. Under Project1 (VbaProject.OTM) expand Microsoft Office Outlook Objects. Double Click on “ThisOutlookSession” and then paste the code in the code pane. Save your work, close Outlook and restart it. Try and test the code by sending yourself some test emails.

I hope this helps some of you.

Leave a Reply