none
Unable to cancel msg send in MailItem.Send RRS feed

  • Question

  • Hello all!  I am having an issue with some code for an Outlook add-in.  I am creating emails, populating the To, Subject, and HTMLBody with details from database records, and then opening the email for the user to view and send upon approval of the content.  I am handling the MailItem.Send event to update the database showing that an email was sent, then get the next record and populate a new message.  This loop continues until there are no more records in the recordset.

    This all works well, but I need to handle exceptions, and have found that when I set the value of Cancel to True, it does not cancel sending the message.  The message is still sent.

    I am using Visual Studio 2010 Ultimate, programming in VB.NET.  Add-in is for Outlook 2010.  Thanks in advance for any assistance!

        Private Sub olMsgTmp_Send(ByRef Cancel As Boolean) Handles olMsgTmp.Send
            Try
                UpdateHeat()
            Catch ex As System.Exception
                'Unable to create a MsgBox from within the event handles for the email, so I have
                'to set the private stEx variable as the exception so it can be handled elsewhere.
                stEx = ex
                Cancel = True
                Exit Sub
            End Try
    
            rs.MoveNext()
            If rs.BOF = False And rs.EOF = False Then
                SetTicketDetails()
                DisplayMessage()
            End If
            Cancel = False
        End Sub


    • Edited by Alan H Gifford Thursday, June 7, 2012 3:32 PM Missed a line
    Thursday, June 7, 2012 3:30 PM

All replies

  • I think it is because you are calling Exit Sub immediately after setting Cancel = True, but I could be wrong.  This does sound familiar and I think it happened to me recently as well.  Try reworking the code to not exit the event if you are cancelling.

    Eric Legault
    MVP (Outlook)
    About me...
    Time-saving Outlook Appins
    for as low as $5!

    Thursday, June 7, 2012 8:10 PM
    Moderator
  • I have tried it without calling Exit Sub, and I get the same result.  In the function UpdateHeat(), I added two lines to the very beginning as such:

    Throw New System.Exception("Error getting system time from SQL server.")
    Exit Sub

    I also just now tried removing Exit Sub from that function, but the results are the same.  I still get the email.

    Thursday, June 7, 2012 10:08 PM
  • I can't explain what is going on - sorry!  All I can suggest is remove everything but Cancel = True and see if that works.

    Eric Legault
    MVP (Outlook)
    About me...
    Time-saving Outlook Appins
    for as low as $5!

    Friday, June 8, 2012 3:21 AM
    Moderator
  • I wonder, could it be that I am currently not running this code as an add-in?  I have created a simple test application that uses this form object as the add-in would, and I haven't had any issues yet.  I just can't be constantly installing and uninstalling the add-in on my work computer as I'm developing this, so the test app makes it much easier.  I'll try this and see if the results are any different.
    Friday, June 8, 2012 3:12 PM
  • I have installed the software as an add-in to Outlook now, and I'm seeing a slightly different behavior that is better, but still not quite what I'm looking for.  Setting Cancel = True now prevents the message being sent, which is nice, but the message inspector stays open.  I tried calling:

    MailItem.Close(Outlook.OlInspectorClose.olDiscard)

    However, I am not allowed to call that from within MailItem.Send.

    Finally, I tried calling 

    MailItem.GetInspector.Close(Outlook.OlInspectorClose.olDiscard)

    It works!  :)

    Thursday, June 14, 2012 1:22 PM
  • It should work in VBA, as well as in .NET.  However, the Cancel argument cannot be used by VBScript - which you are not using.  So the mystery remains.

    Eric Legault
    MVP (Outlook)
    About me...
    Time-saving Outlook Appins
    for as low as $5!

    Thursday, June 14, 2012 4:10 PM
    Moderator
  • Setting Cancel to True is not supposed to close the form.  The idea is to give the user an opportunity to continue working in the form.

    FYI, I'm unmarking your response as an answer because it is considered very bad taste to mark responses to your own question as an answer - to prevent people from running up forum points.


    Eric Legault
    MVP (Outlook)
    About me...
    Time-saving Outlook Appins
    for as low as $5!

    Thursday, June 14, 2012 4:12 PM
    Moderator
  • I see.  Sorry about that.  I thought I was doing the forum a favor by marking the thread as a resolved issue, making it more likely that someone in the future with the same problem will find their answer here.

    On the issue of the remaining mystery: are you saying that even though I was previously using this object as a standalone program rather than an Outlook add-in, the Cancel setting still should have worked?

    Thursday, June 14, 2012 8:27 PM
  • No problem about marking it as an answer.  I'm probably being too strict unmarking it, but sometimes these Moderator responsibilities get to my head LOL!  I err on following the rules and following protocol.

    If the only difference in your code is that add-in code works vs. out-of-process, than perhaps this is indeed a restriction of using the Cancel event (similar to VBScript).  It is not documented that you can't use it, but there you go.  Maybe it is a bug.  If I have time I'll try to repro, or maybe Ken or Dmitry can step in and give their input.


    Eric Legault
    MVP (Outlook)
    About me...
    Time-saving Outlook Appins
    for as low as $5!

    Thursday, June 14, 2012 9:02 PM
    Moderator