Consulting

Results 1 to 7 of 7

Thread: Solved: Check Subject field and body

  1. #1
    VBAX Regular
    Joined
    Aug 2005
    Posts
    77
    Location

    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

  2. #2
    Knowledge Base Approver
    The King of Overkill!
    VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    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

  3. #3
    VBAX Regular
    Joined
    Aug 2005
    Posts
    77
    Location
    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

  4. #4
    VBAX Mentor tpoynton's Avatar
    Joined
    Feb 2005
    Location
    Clinton, MA
    Posts
    399
    Location
    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]

  5. #5
    Knowledge Base Approver
    The King of Overkill! VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    Adding vbsystemmodal is what I was going to say, sorry it took so long. Thanks for picking up my slack

  6. #6
    VBAX Regular
    Joined
    Aug 2005
    Posts
    77
    Location
    it works now fine ... but before i must remove the MsgBox brackets maybe it depends my local settings

  7. #7
    Knowledge Base Approver
    The King of Overkill! VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    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
  •