-
Solved: Check Subject field and body
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
-
Knowledge Base Approver
The King of Overkill!
VBAX Master
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:[vba]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[/vba]
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
-
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
-
I actually struggled with this a bit too. hopefully adding the vbSystemModal option to the messagebox will work for you.
[vba]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[/vba]
-
Knowledge Base Approver
The King of Overkill!
VBAX Master
Adding vbsystemmodal is what I was going to say, sorry it took so long. Thanks for picking up my slack
-
it works now fine ... but before i must remove the MsgBox brackets maybe it depends my local settings
-
Knowledge Base Approver
The King of Overkill!
VBAX Master
Yes, you could (may error out otherwise). You would want to use the parenthesis if you were doing something like[vba]if msgbox("yes or no",vbyesno + vbsystemmodal)=vbyes then[/vba]etc, otherwise they wouldnt be needed.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules