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:
Then the code behind the Submit button as follows:
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!
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!