PDA

View Full Version : Solved: Check Subject field and body



Cass
10-17-2006, 11:07 AM
Hello!

Is it possible to check before sending mail subject field length and body length? and if subject field text is too long or/and body is blank block sending this mail.

Reason why i need this is blocking users sending mil where all text is writing subject field :banghead:

mvidas
10-17-2006, 11:27 AM
Hi Cass,
Why not?
Press Alt-F11 from outlook to open the VBA editor. Press control-r to open/focus on the Project window, and expand the "Microsoft Outlook Objects" folder. Double click on "ThisOutlookSession", and paste the following into the code pane for that:Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
If TypeName(Item) <> "MailItem" Then Exit Sub 'only process mailitems
If Len(Item.Subject) > 60 Then 'specify max subject length
MsgBox "Subject is too long."
Cancel = True
Exit Sub
End If
If Len(Item.Body) < 2 Then
MsgBox "Message text is blank."
Cancel = True
Exit Sub
End If
End Sub
Set the max subject length, and save/close the vba editor. Now from outlook go to Tools / Macro / Security, and change the settings to Medium if they're not there already (it will prompt you each time you open outlook to enable macros--I can give you information on avoiding this if you'd like).
That should take care of it!
Matt

Cass
10-17-2006, 12:58 PM
excellent :)
but ... maybe is better when msgbox appear top of the new mail item. At the moment it appears background and user don't know the message before try open Outlook Main window.

Outlook 11 is installed ;)

tpoynton
10-17-2006, 07:07 PM
I actually struggled with this a bit too. hopefully adding the vbSystemModal option to the messagebox will work for you.

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
If TypeName(Item) <> "MailItem" Then Exit Sub 'only process mailitems
If Len(Item.Subject) > 60 Then 'specify max subject length
MsgBox ("Subject is too long.", vbSystemModal)
Cancel = True
Exit Sub
End If
If Len(Item.Body) < 2 Then
MsgBox ("Message text is blank.", vbSystemModal)
Cancel = True
Exit Sub
End If
End Sub

mvidas
10-18-2006, 10:58 AM
Adding vbsystemmodal is what I was going to say, sorry it took so long. Thanks for picking up my slack :)

Cass
10-18-2006, 11:04 AM
:) it works now fine ... but before i must remove the MsgBox brackets :think: maybe it depends my local settings

mvidas
10-18-2006, 11:17 AM
Yes, you could (may error out otherwise). You would want to use the parenthesis if you were doing something likeif msgbox("yes or no",vbyesno + vbsystemmodal)=vbyes thenetc, otherwise they wouldnt be needed.