VBA: No Subject, No Body then No send


 

 

 

What the macro do
I need this macro when my boss ask me for a way to prevent the internal user from sending mails to the firms partners with no subject or body text, This macro is installed in all the firms computers including my own ☺.

 

Note
On this page I will not cover how you get it installed on all the computers in you network or how you turn the Microsoft outlook security options on/off, maybe some day I will make a page for it, if some one ask for it

 

The Setup
It is a Windows 2000 AD network with Exchange 2000 and the client side is Windows XP with Office XP

 

 

The Macro
Okay here we go, first you start Outlook and then you hit Alt-F11 to start the macro VBA editor

Under the Project you see the ThisOutlookSession, This is where we put the code into

 

This is the code to use, copy/paste the code into the VBA editor

 

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


        'No hard error thanks
       
On Error Resume Next


        'The ActiveInspector is the currently open item.
        Set objInsp = Outlook.Application.ActiveInspector


        'Getting the items class
        Err.Clear
        CurrentItem_Class = objInsp.CurrentItem.Class
        If Err.Number <> 0 Then
                CurrentItem_Class = 0
                Err.Clear
        End If


        'verify that Word is not the mail editor
        Err.Clear
        Maileditor = objInsp.IsWordMail
        If Err.Number <> 0 Then
                Maileditor = False
                Err.Clear
        End If


        'Is the subject empty
        Err.Clear
        'verify the item is a mail and not some thing else
        If CurrentItem_Class = 43 And Maileditor = False Then

                'testing the subjekt
                Mail_subject = objInsp.CurrentItem.Subject
                If Len(Mail_subject) = 0 And Err.Number = 0 Then
                            str_null = MsgBox("There is no subject!!!", 0, "No subject")
                            Cancel = True
                            Err.Clear
                            Exit Sub
                End If

                'getting the mail format
                Err.Clear
                EditorType = objInsp.CurrentItem.GetInspector.EditorType

                'tester Body (If Plain text(1) or Rich text(3))
                If EditorType = 1 Or EditorType = 3 Then
                            bodytext = Trim(Replace(objInsp.CurrentItem.Body, " ", ""))
                            min_body_size = 1
                End If
                'tester Body (If HTML(2))
                If EditorType = 2 Then
                            bodytext = Trim(Replace(objInsp.CurrentItem.Body, " ", ""))
                            bodytext = Replace(bodytext, "&nbsp;", "")
                            min_body_size = 3
                End If

                'testing the body size
                If Len(bodytext) < min_body_size And Err.Number = 0 Then
                            str_null = MsgBox("There is no body text!!!", 0, "No body text!")
                            Cancel = True
                            Err.Clear
                            Exit Sub
                End If

        Err.Clear
        End If


        'closing the objects
        Set objInsp = Nothing


        'return to normal error mode
        On Error GoTo 0

End Sub
 

 

When you have done that, you will see some thing like this.

Now you just save the VBA project and close the editor, so you get back to outlook.

 

When you now make a new mail and forget to enter a subject line, you will not be allow to send and a nice dialog will popup


yah yah, now you enter a subject and try send again.....
 

click OK to close the dialog box, enter a body text and send the mail.......

So now your users only can send nice format mails and the mail receiver is a bit more happy

Download the code here (in a txt file)

Thomas