Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2003
    Location
    London
    Posts
    341

    Unanswered: If Send Email is cancelled...

    Hi all,

    I'm currently working on a excel sheet which emails key information on a button push. The code that I'm using is

    Code:
        Dim OutApp As Object
            Set OutApp = CreateObject("Outlook.Application")
    
        Dim OutMail As Object
            Set OutMail = OutApp.CreateItem(0)
    
        Dim StrSubject As String
        Dim StrBody As String
    
            StrSubject = "This is the subject line."
            Strbody = "This is the body"
    
            On Error Resume Next
    
            With OutMail
                .to = ""
                .CC = ""
                .BCC = ""
                .Subject = StrSubject
                .Body = Strbody
                .BodyFormat = 3 '1=Plain text, 2=HTML 3=RichText
                .Display   'or use .Send
            End With
    
            On Error GoTo 0
    
        Set OutMail = Nothing
        Set OutApp = Nothing
    
    End Sub
    I'm not used to this method and am more familiar with the DoCmd.SendObject but I'm trying to learn how to do processes in various ways.

    When I used the DoCmd.SendObject though, if I cancel the email, the process comes to a halt.

    In the method I'm using, when I cancel the email, the process continues. How do I halt the process in the above method? What I want is if someone presses the send button by accident, I want them to be able to cancel the email, and for the process to completely stop rather than doing the next bit.

    Any help would be appreciated. Thank you.

  2. #2
    Join Date
    May 2009
    Posts
    258
    Hello christyxo,

    Just use Exit Sub if you cancel the email.

    Regards,

    Ax

  3. #3
    Join Date
    Oct 2003
    Location
    London
    Posts
    341
    Thank you but I'm having trouble even with adding an Exit Sub.

    At the end of my sub I now have the following

    Code:
    Exit_Sub:
        Exit Sub
    
    Err_Sub:
        MsgBox Err.Description
        Resume Exit_Sub
    and now, all over the place I'm putting in lines which say

    On Error GoTo Err_Sub, or even GoTo Exit_Sub, but it doesn't seem to having any effect. It's as if cancelling the email is not causing an error, or doing enough to justify halting the process.
    Last edited by christyxo; 03-02-12 at 07:14. Reason: My grammar was appalling!

  4. #4
    Join Date
    May 2009
    Posts
    258
    I see that you are using Outlook to send the mail. If that is the case, I don't think this code will even matter in Excel, because you are now dealing with an Outlook problem, rather than an Excel scripting problem. Please tell me in what interface you are canceling the email.

  5. #5
    Join Date
    Oct 2003
    Location
    London
    Posts
    341
    Thank you Ax238. Sorry for my delay but I called into another project.

    I'm using Outlook and wished to cancel the process within Outlook. I didn't even consider the fact that I was using a different application.

    I resolved the situation by creating a Msgbox at the beginning stating that "the following email is this..., are you sure you want to send it"

    It gets around the problem...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •