created email in VB, user clicks on cancel.

SlinkyWheels

Board Regular
Joined
Jan 5, 2007
Messages
217
Hi

I have some vb code that creates an email with an attachment.

How do I identify whether the user has clicked on "Send" or on the X to close the email down without sending it?
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Can you post the coding up? You can set it so the email sends automatically unless you want the choice?

The coding I use puts a timer that says the email will send in 5 seconds. If the user wants to cancel they can otherwise it sends on its own
 
Upvote 0
Whoops!!! Sorry.

If it sends I need it to do one thing, if they click on cancel, I need it to do another.

Code:
Private Sub cmdYes_Click()
On Error GoTo ErrorHandler

Dim FileExtStr As String
Dim FileFormatNum As Long
'Dim Sourcewb As Workbook
Dim Destwb As Workbook
Dim TempFilePath As String
Dim TempFileName As String
Dim OutApp As Object
Dim OutMail As Object
Dim MsgPart1, MsgPart2, MsgPart3 As String

Application.ScreenUpdating = False

Company = Application.OrganizationName

MsgPart1 = vbCrLf & "Please find attached the latest copy of the Appendix B Data." & vbCrLf & vbCrLf
MsgPart2 = "Kind regards," & vbCrLf & vbCrLf
MsgPart3 = Company

MsgBody = MsgPart1 & MsgPart2 & MsgPart3

'copy the sheet to a new workbook and delete all previously sent data

ActiveWorkbook.Unprotect Password:="PassApp"

Sheets("AppBDataSheet").Visible = True
Sheets("AppBDataSheet").Unprotect "PassAppB"

Sheets("AppBDataSheet").Select

            With Sheets("AppBDataSheet")
            .Range("A1").CurrentRegion.Sort key1:=.Range("AH2"), Order1:=xlAscending, _
            Header:=xlYes, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
            End With

ActiveSheet.Copy
Set Destwb = ActiveWorkbook

            Range("AH2").Select
            
Step1:
            Do Until ActiveCell.Value = Blank
                Set F = ActiveCell.Find("Yes", LookAt:=xlPart, LookIn:=xlValues)
                If F = "Yes" Then
                ActiveCell.EntireRow.Delete
                GoTo Step1
                End If
                ActiveCell.Offset(1, 0).Select
            Loop
            
            Range("A1").Select

'Excel Version??

With Destwb
FileExtStr = ".xls": FileFormatNum = -4143
End With

'Save new workbook, create and send email, delete this new workbook
TempFilePath = Environ$("temp") & "\"
TempFileName = Company & " - Appendix B Data"

Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)

With Destwb
    .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
    On Error Resume Next
    With OutMail
        .to = "Legal.Panel@southwestrda.org.uk"
        .Subject = TempFileName
        .Body = MsgBody
        .Attachments.Add Destwb.FullName
        .Display
        End With
    On Error GoTo 0
    .Close SaveChanges:=False
End With

'Delete the new worksheet
Kill TempFilePath & TempFileName & FileExtStr

Set OutMail = Nothing
Set OutApp = Nothing

'add YES to Column AH for all emailed info
LstRw = Cells(Rows.Count, "A").End(xlUp).Row

Range("AH2").Select

'Do Until ActiveCell.Row > LstRw + 1
Do Until ActiveCell.Row > LstRw

    If ActiveCell.Value = "Yes" Then
    ActiveCell.Offset(1, 0).Select
    End If
    If ActiveCell.Value = Blank Then
    ActiveCell.Value = "Yes"
    ActiveCell.Offset(1, 0).Select
    End If
Loop

Sheets("AppBDataSheet").Protect "PassAppB"
Sheets("AppBDataSheet").Visible = False
ActiveWorkbook.Protect Password:="PassApp"
Application.ScreenUpdating = True
Sheets("Front Screen").Select
Unload Me
UFmainmenu.Show
ErrorHandler:
End Sub
 
Upvote 0
Replace

Rich (BB code):
With Destwb 
    .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum 
    On Error Resume Next 
    With OutMail 
        .to = "Legal.Panel@southwestrda.org.uk" 
        .Subject = TempFileName 
        .Body = MsgBody 
        .Attachments.Add Destwb.FullName 
        .Display 
        End With 
    On Error GoTo 0 
    .Close SaveChanges:=False 
End With

with

Rich (BB code):
Dim UserResponse As Integer

UserResponse = MsgBox ("Are you sure you want to send this?", vbYesNo)

If UserResponse = vbYes Then

With Destwb 
    .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum 
    On Error Resume Next 
    With OutMail 
        .to = "Legal.Panel@southwestrda.org.uk" 
        .Subject = TempFileName 
        .Body = MsgBody 
        .Attachments.Add Destwb.FullName 
        .Send
        End With 
    On Error GoTo 0 
    .Close SaveChanges:=False 
End With 


Else

{Do something else}

End if
 
Upvote 0
That kind of works (so thankyou for that) but it now brings up a box telling me that a programme is trying to access Outlook.

Is there any way around this? I did not get this message when sending the email in my original code
 
Upvote 0
Try this

Code:
Private Sub cmdYes_Click() 
On Error GoTo ErrorHandler 

Dim FileExtStr As String 
Dim FileFormatNum As Long 
'Dim Sourcewb As Workbook 
Dim Destwb As Workbook 
Dim TempFilePath As String 
Dim TempFileName As String 
Dim OutApp As Object 
Dim OutMail As Object 
Dim MsgPart1, MsgPart2, MsgPart3 As String 

Application.ScreenUpdating = False 

Company = Application.OrganizationName 

MsgPart1 = vbCrLf & "Please find attached the latest copy of the Appendix B Data." & vbCrLf & vbCrLf 
MsgPart2 = "Kind regards," & vbCrLf & vbCrLf 
MsgPart3 = Company 

MsgBody = MsgPart1 & MsgPart2 & MsgPart3 

'copy the sheet to a new workbook and delete all previously sent data 

ActiveWorkbook.Unprotect Password:="PassApp" 

Sheets("AppBDataSheet").Visible = True 
Sheets("AppBDataSheet").Unprotect "PassAppB" 

Sheets("AppBDataSheet").Select 

            With Sheets("AppBDataSheet") 
            .Range("A1").CurrentRegion.Sort key1:=.Range("AH2"), Order1:=xlAscending, _ 
            Header:=xlYes, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom 
            End With 

ActiveSheet.Copy 
Set Destwb = ActiveWorkbook 

            Range("AH2").Select 
            
Step1: 
            Do Until ActiveCell.Value = Blank 
                Set F = ActiveCell.Find("Yes", LookAt:=xlPart, LookIn:=xlValues) 
                If F = "Yes" Then 
                ActiveCell.EntireRow.Delete 
                GoTo Step1 
                End If 
                ActiveCell.Offset(1, 0).Select 
            Loop 
            
            Range("A1").Select 

'Excel Version?? 

With Destwb 
FileExtStr = ".xls": FileFormatNum = -4143 
End With 

'Save new workbook, create and send email, delete this new workbook 
TempFilePath = Environ$("temp") & "\" 
TempFileName = Company & " - Appendix B Data" 

Set OutApp = CreateObject("Outlook.Application") 
OutApp.Session.Logon 
Set OutMail = OutApp.CreateItem(0) 

Dim UserResponse As Integer 

UserResponse = MsgBox ("Are you sure you want to send this?", vbYesNo) 

If Not UserResponse = vbYes Then Exit Sub
Else

With Destwb 
    .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum 
    On Error Resume Next 
    With OutMail 
        .to = "Legal.Panel@southwestrda.org.uk" 
        .Subject = TempFileName 
        .Body = MsgBody 
        .Attachments.Add Destwb.FullName 
        .Display
        End With 
    On Error GoTo 0 
    .Close SaveChanges:=False 
End With 
SendKeys "%{s}", True

'Delete the new worksheet 
Kill TempFilePath & TempFileName & FileExtStr 

Set OutMail = Nothing 
Set OutApp = Nothing 

'add YES to Column AH for all emailed info 
LstRw = Cells(Rows.Count, "A").End(xlUp).Row 

Range("AH2").Select 

'Do Until ActiveCell.Row > LstRw + 1 
Do Until ActiveCell.Row > LstRw 

    If ActiveCell.Value = "Yes" Then 
    ActiveCell.Offset(1, 0).Select 
    End If 
    If ActiveCell.Value = Blank Then 
    ActiveCell.Value = "Yes" 
    ActiveCell.Offset(1, 0).Select 
    End If 
Loop 

Sheets("AppBDataSheet").Protect "PassAppB" 
Sheets("AppBDataSheet").Visible = False 
ActiveWorkbook.Protect Password:="PassApp" 
Application.ScreenUpdating = True 
Sheets("Front Screen").Select 
Unload Me 
UFmainmenu.Show 
ErrorHandler: 
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,867
Messages
6,122,002
Members
449,059
Latest member
mtsheetz

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top