Outlook 2010 Preventing the Send button from being clicked on an Outlook Form

humanscar

New Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
I have many Outlook forms that I have "inherited" and most use code to check the form has been completed properly, then construct an email based on the contents of the form. The reason for this is to ensure that the email sent as a result of submitting the form is in a standard format that can then be "read" by various automation tools. It's a clanky approach I know, but I can't change things (politics).

So the method used when we were using 2003 doesn't appear to work in 2010... and this is how most have been written:
Code:
Sub Item_Send()

    If SendFlag = 1 Then
        MsgBox "Please use the Submit button at the bottom of this page"
      exit sub
    Else
      Item.Send
    End If
End Sub
Then the code behind the Submit button as follows:
Code:
Sub cmdSubmit_Click()

    Set csprps = Item.UserProperties
    Set csitm = Item.GetInspector
    Set cspgs = csitm.ModifiedFormPages
    Set cspg = cspgs("Form")
    Set csctls = cspg.Controls

    'Set Send Flag
    SendFlag = 1
   
     ' Loads of code to validate the field data etc.

'Build message body
       varSubject = "New Request"
       varReceipient = "relevant mailbox"
       varMessageBody = "Please process the following request" & vbCrLf & vbCrLf & _
          "001 Name: " & csprps("A001") & vbCrLf
        'loads more code to build the message body

    'Build up and send the message
    Set objOutlook = CreateObject("Outlook.application")
    Set mItem = objOutlook.CreateItem(olMailItem)
    mItem.To = varReceipient
    mItem.Subject = varSubject
    mItem.Body = varMessageBody
    SendFlag = 2
    mItem.Send

    ' ****  Clean up
    Set mItem = Nothing
    Set objOutlook = Nothing
    ' Close item without saving
    Item.Close (1)

End Sub
The issue is that the Item_Send code runs (when either Send or Submit is clicked) but doesn't prevent the form from being sent when the user has clicked Send rather than Submit. It does show the message box when Send is clicked and not when Submit is clicked, but the exit Sub doesn't stop it from being sent.
It then arrives as a form rather than the formatted text that the code behind the submit button would send.

Is there a way I can cancel the "Send" either easily or more elegantly?

Many thanks in anticipation!
 

Michael Bauer

Senior Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
At a first glance I don't understand why it has worked at all, so maybe I'm missing anything. It's VB Script, not VBA, isn't it?

I'm not familar with VBS, but Item_Send is a function, not a sub (at least in OL 07, and I don't think it's been changed). The difference is that a function returns a value. I guess, you need to return False if you want to cancel the sending process, or return True if you want to proceed.

As Item_Send is being called when a message is going to be sent, I don't get why Item.Send is called (again) within that procedure.
 
Top