Thursday, January 20, 2011

Check for Attachment and Empty Subject Line in MS Outlook

Hi,


By mistake, many of us send out emails without a subject or forget attaching when we should have. Following is the code which will help you check for both of these. It is very simple and straightforward. All you need to do is follow the following steps:

1. Open outlook and press Alt + F11. This will open the Microsoft Visual Basic editor.
2. On the left side you would see Project Pane. Click on Project 1 > Microsoft Office Outlook > This Outlook Session.
3. On the right side just copy paste the following VBA code and then press Ctrl + S to save the code and you are done:

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim strSubject As String
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
If InStr(1, Item.Body, "attach", vbTextCompare) > 0 Then
Prompt$ = "Seems you have forgotten to attach a document. Do you want to send an email anyways?"
If MsgBox(Prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check for Attachment") = vbNo Then
Cancel = True
End If
End If
End Sub