Stop Outlook VBA from sending mail within ItemSend() when an error event has occurred










0















Outlook mailitem gets sent when an error occurs during run-time within an ItemSend() event. Placing Cancel = True in the code does not even stop this from happening. Is it an inherent VBA flaw?



How would I get around this problem? Any ideas are welcome.



Public WithEvents myApp As Outlook.Application

Sub Initialize_handler()
Set myApp = Application
End Sub

Private Sub myApp_ItemSend(ByVal Item As Object, Cancel As Boolean)
On Error GoTo ErrorHandler_myApp_ItemSend
Cancel = True

' Do something erroneous

Exit Sub

ErrorHandler_myApp_ItemSend:
Cancel = True
MsgBox "Error: " Err.Description
Err.Clear
End Sub









share|improve this question
























  • Put a breakpoint on both Cancel = True lines, and run your code. Does the code reach either?

    – Excelosaurus
    Nov 14 '18 at 2:59






  • 1





    If all this code is in ThisOutlookSession would you edit the question to remove unneeded complications, by removing the first four lines and changing to Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean). Would you put in an Err.Raise so potential respondents do not introduce mistakes trying to create an error.

    – niton
    Nov 14 '18 at 14:56












  • nitro@ thanks for the tip.

    – Barok
    Nov 25 '18 at 5:26















0















Outlook mailitem gets sent when an error occurs during run-time within an ItemSend() event. Placing Cancel = True in the code does not even stop this from happening. Is it an inherent VBA flaw?



How would I get around this problem? Any ideas are welcome.



Public WithEvents myApp As Outlook.Application

Sub Initialize_handler()
Set myApp = Application
End Sub

Private Sub myApp_ItemSend(ByVal Item As Object, Cancel As Boolean)
On Error GoTo ErrorHandler_myApp_ItemSend
Cancel = True

' Do something erroneous

Exit Sub

ErrorHandler_myApp_ItemSend:
Cancel = True
MsgBox "Error: " Err.Description
Err.Clear
End Sub









share|improve this question
























  • Put a breakpoint on both Cancel = True lines, and run your code. Does the code reach either?

    – Excelosaurus
    Nov 14 '18 at 2:59






  • 1





    If all this code is in ThisOutlookSession would you edit the question to remove unneeded complications, by removing the first four lines and changing to Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean). Would you put in an Err.Raise so potential respondents do not introduce mistakes trying to create an error.

    – niton
    Nov 14 '18 at 14:56












  • nitro@ thanks for the tip.

    – Barok
    Nov 25 '18 at 5:26













0












0








0








Outlook mailitem gets sent when an error occurs during run-time within an ItemSend() event. Placing Cancel = True in the code does not even stop this from happening. Is it an inherent VBA flaw?



How would I get around this problem? Any ideas are welcome.



Public WithEvents myApp As Outlook.Application

Sub Initialize_handler()
Set myApp = Application
End Sub

Private Sub myApp_ItemSend(ByVal Item As Object, Cancel As Boolean)
On Error GoTo ErrorHandler_myApp_ItemSend
Cancel = True

' Do something erroneous

Exit Sub

ErrorHandler_myApp_ItemSend:
Cancel = True
MsgBox "Error: " Err.Description
Err.Clear
End Sub









share|improve this question
















Outlook mailitem gets sent when an error occurs during run-time within an ItemSend() event. Placing Cancel = True in the code does not even stop this from happening. Is it an inherent VBA flaw?



How would I get around this problem? Any ideas are welcome.



Public WithEvents myApp As Outlook.Application

Sub Initialize_handler()
Set myApp = Application
End Sub

Private Sub myApp_ItemSend(ByVal Item As Object, Cancel As Boolean)
On Error GoTo ErrorHandler_myApp_ItemSend
Cancel = True

' Do something erroneous

Exit Sub

ErrorHandler_myApp_ItemSend:
Cancel = True
MsgBox "Error: " Err.Description
Err.Clear
End Sub






vba events outlook onerror






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 2:05







Barok

















asked Nov 14 '18 at 1:41









BarokBarok

508




508












  • Put a breakpoint on both Cancel = True lines, and run your code. Does the code reach either?

    – Excelosaurus
    Nov 14 '18 at 2:59






  • 1





    If all this code is in ThisOutlookSession would you edit the question to remove unneeded complications, by removing the first four lines and changing to Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean). Would you put in an Err.Raise so potential respondents do not introduce mistakes trying to create an error.

    – niton
    Nov 14 '18 at 14:56












  • nitro@ thanks for the tip.

    – Barok
    Nov 25 '18 at 5:26

















  • Put a breakpoint on both Cancel = True lines, and run your code. Does the code reach either?

    – Excelosaurus
    Nov 14 '18 at 2:59






  • 1





    If all this code is in ThisOutlookSession would you edit the question to remove unneeded complications, by removing the first four lines and changing to Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean). Would you put in an Err.Raise so potential respondents do not introduce mistakes trying to create an error.

    – niton
    Nov 14 '18 at 14:56












  • nitro@ thanks for the tip.

    – Barok
    Nov 25 '18 at 5:26
















Put a breakpoint on both Cancel = True lines, and run your code. Does the code reach either?

– Excelosaurus
Nov 14 '18 at 2:59





Put a breakpoint on both Cancel = True lines, and run your code. Does the code reach either?

– Excelosaurus
Nov 14 '18 at 2:59




1




1





If all this code is in ThisOutlookSession would you edit the question to remove unneeded complications, by removing the first four lines and changing to Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean). Would you put in an Err.Raise so potential respondents do not introduce mistakes trying to create an error.

– niton
Nov 14 '18 at 14:56






If all this code is in ThisOutlookSession would you edit the question to remove unneeded complications, by removing the first four lines and changing to Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean). Would you put in an Err.Raise so potential respondents do not introduce mistakes trying to create an error.

– niton
Nov 14 '18 at 14:56














nitro@ thanks for the tip.

– Barok
Nov 25 '18 at 5:26





nitro@ thanks for the tip.

– Barok
Nov 25 '18 at 5:26












2 Answers
2






active

oldest

votes


















1














I have no explanation why the mail is not sent when closing the inspector.



Option Explicit

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)

Dim objInsp As Inspector

On Error GoTo ErrorHandler_Application_ItemSend

' Do something erroneous
Err.Raise 1

Exit Sub

ErrorHandler_Application_ItemSend:

Cancel = True
MsgBox "Error: " & Err.Description

Set objInsp = Item.GetInspector

' Look for the mail in the drafts folder
objInsp.Close olSave

End Sub





share|improve this answer























  • Thanks for providing code. It is mind boggling, because suddenly the code seems to work fine again. I can't be sure but it may perhaps have nothing to do with the problem asked. I will have to wait and see if the problem re-surfaces.

    – Barok
    Nov 25 '18 at 5:23



















-1














You couldn’t use On Error GoTo to get error event, you could try to use If statement. For example:



Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)

On Error Resume Next

If Not Left(LCase(Item.Subject), 3) = "re:" And Not Left(LCase(Item.Subject), 3) = "fw:" Then
prompt$ = "You sending this from " & Item.SendUsingAccount & ". Are you sure you want to send it?"
If MsgBox(prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check Sending Account") = vbNo Then
Cancel = True
End If
End If

End Sub


For more information, please refer to this link:



Warn Before Sending Messages to the Wrong Email Address



Application.ItemSend Event (Outlook)






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291978%2fstop-outlook-vba-from-sending-mail-within-itemsend-when-an-error-event-has-occ%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    I have no explanation why the mail is not sent when closing the inspector.



    Option Explicit

    Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)

    Dim objInsp As Inspector

    On Error GoTo ErrorHandler_Application_ItemSend

    ' Do something erroneous
    Err.Raise 1

    Exit Sub

    ErrorHandler_Application_ItemSend:

    Cancel = True
    MsgBox "Error: " & Err.Description

    Set objInsp = Item.GetInspector

    ' Look for the mail in the drafts folder
    objInsp.Close olSave

    End Sub





    share|improve this answer























    • Thanks for providing code. It is mind boggling, because suddenly the code seems to work fine again. I can't be sure but it may perhaps have nothing to do with the problem asked. I will have to wait and see if the problem re-surfaces.

      – Barok
      Nov 25 '18 at 5:23
















    1














    I have no explanation why the mail is not sent when closing the inspector.



    Option Explicit

    Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)

    Dim objInsp As Inspector

    On Error GoTo ErrorHandler_Application_ItemSend

    ' Do something erroneous
    Err.Raise 1

    Exit Sub

    ErrorHandler_Application_ItemSend:

    Cancel = True
    MsgBox "Error: " & Err.Description

    Set objInsp = Item.GetInspector

    ' Look for the mail in the drafts folder
    objInsp.Close olSave

    End Sub





    share|improve this answer























    • Thanks for providing code. It is mind boggling, because suddenly the code seems to work fine again. I can't be sure but it may perhaps have nothing to do with the problem asked. I will have to wait and see if the problem re-surfaces.

      – Barok
      Nov 25 '18 at 5:23














    1












    1








    1







    I have no explanation why the mail is not sent when closing the inspector.



    Option Explicit

    Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)

    Dim objInsp As Inspector

    On Error GoTo ErrorHandler_Application_ItemSend

    ' Do something erroneous
    Err.Raise 1

    Exit Sub

    ErrorHandler_Application_ItemSend:

    Cancel = True
    MsgBox "Error: " & Err.Description

    Set objInsp = Item.GetInspector

    ' Look for the mail in the drafts folder
    objInsp.Close olSave

    End Sub





    share|improve this answer













    I have no explanation why the mail is not sent when closing the inspector.



    Option Explicit

    Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)

    Dim objInsp As Inspector

    On Error GoTo ErrorHandler_Application_ItemSend

    ' Do something erroneous
    Err.Raise 1

    Exit Sub

    ErrorHandler_Application_ItemSend:

    Cancel = True
    MsgBox "Error: " & Err.Description

    Set objInsp = Item.GetInspector

    ' Look for the mail in the drafts folder
    objInsp.Close olSave

    End Sub






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 19 '18 at 22:36









    nitonniton

    5,52571942




    5,52571942












    • Thanks for providing code. It is mind boggling, because suddenly the code seems to work fine again. I can't be sure but it may perhaps have nothing to do with the problem asked. I will have to wait and see if the problem re-surfaces.

      – Barok
      Nov 25 '18 at 5:23


















    • Thanks for providing code. It is mind boggling, because suddenly the code seems to work fine again. I can't be sure but it may perhaps have nothing to do with the problem asked. I will have to wait and see if the problem re-surfaces.

      – Barok
      Nov 25 '18 at 5:23

















    Thanks for providing code. It is mind boggling, because suddenly the code seems to work fine again. I can't be sure but it may perhaps have nothing to do with the problem asked. I will have to wait and see if the problem re-surfaces.

    – Barok
    Nov 25 '18 at 5:23






    Thanks for providing code. It is mind boggling, because suddenly the code seems to work fine again. I can't be sure but it may perhaps have nothing to do with the problem asked. I will have to wait and see if the problem re-surfaces.

    – Barok
    Nov 25 '18 at 5:23














    -1














    You couldn’t use On Error GoTo to get error event, you could try to use If statement. For example:



    Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)

    On Error Resume Next

    If Not Left(LCase(Item.Subject), 3) = "re:" And Not Left(LCase(Item.Subject), 3) = "fw:" Then
    prompt$ = "You sending this from " & Item.SendUsingAccount & ". Are you sure you want to send it?"
    If MsgBox(prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check Sending Account") = vbNo Then
    Cancel = True
    End If
    End If

    End Sub


    For more information, please refer to this link:



    Warn Before Sending Messages to the Wrong Email Address



    Application.ItemSend Event (Outlook)






    share|improve this answer



























      -1














      You couldn’t use On Error GoTo to get error event, you could try to use If statement. For example:



      Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)

      On Error Resume Next

      If Not Left(LCase(Item.Subject), 3) = "re:" And Not Left(LCase(Item.Subject), 3) = "fw:" Then
      prompt$ = "You sending this from " & Item.SendUsingAccount & ". Are you sure you want to send it?"
      If MsgBox(prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check Sending Account") = vbNo Then
      Cancel = True
      End If
      End If

      End Sub


      For more information, please refer to this link:



      Warn Before Sending Messages to the Wrong Email Address



      Application.ItemSend Event (Outlook)






      share|improve this answer

























        -1












        -1








        -1







        You couldn’t use On Error GoTo to get error event, you could try to use If statement. For example:



        Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)

        On Error Resume Next

        If Not Left(LCase(Item.Subject), 3) = "re:" And Not Left(LCase(Item.Subject), 3) = "fw:" Then
        prompt$ = "You sending this from " & Item.SendUsingAccount & ". Are you sure you want to send it?"
        If MsgBox(prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check Sending Account") = vbNo Then
        Cancel = True
        End If
        End If

        End Sub


        For more information, please refer to this link:



        Warn Before Sending Messages to the Wrong Email Address



        Application.ItemSend Event (Outlook)






        share|improve this answer













        You couldn’t use On Error GoTo to get error event, you could try to use If statement. For example:



        Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)

        On Error Resume Next

        If Not Left(LCase(Item.Subject), 3) = "re:" And Not Left(LCase(Item.Subject), 3) = "fw:" Then
        prompt$ = "You sending this from " & Item.SendUsingAccount & ". Are you sure you want to send it?"
        If MsgBox(prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check Sending Account") = vbNo Then
        Cancel = True
        End If
        End If

        End Sub


        For more information, please refer to this link:



        Warn Before Sending Messages to the Wrong Email Address



        Application.ItemSend Event (Outlook)







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 8:21









        Alina LiAlina Li

        633125




        633125



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291978%2fstop-outlook-vba-from-sending-mail-within-itemsend-when-an-error-event-has-occ%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

            Syphilis

            Darth Vader #20