Add "Ok/Cancel" message box to my email script

GamerNeelie

New Member
Joined
May 21, 2022
Messages
25
Office Version
  1. 2013
Platform
  1. Windows
I have a working email script that I've been using for sometime.

Once the user clicks a button it will run the script and open a new window for the email

However i would like a message box to appear asking the user if they would like to continue with the email (If they clicked it by mistake etc)

I just unsure where in the script I would add it for it to work correctly

Example of the kind of box i want to add
VBA Code:
Dim answer As Integer
answer = MsgBox("Are you sure you wish to email", vbOKCancel)

Else
    MsgBox "Email Cancelled"

Example of my Email Script:
VBA Code:
Sub Email1()
    Dim OutApp As Object
    Dim OutMail As Object
    Dim NewWB As String
    Dim filename1 As String

    filename1 = Sheet2.Range("AF20").Value & " - " & Sheet2.Range("AC21").Value & " " & Sheet2.Range("AC22").Value & " - " & Sheet2.Range("AC23").Value

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

    'On Error Resume Next
    
    With OutMail
        .To = Range("AJ18").Value
        .CC = "******"
        .BCC = ""
        .Subject = "********
        .Body = "*********

        .Attachments.Add ThisWorkbook.Path & Application.PathSeparator & filename1 & ".xlsm"
        
        '.Send
        .Display
    End With
    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing
    
    Kill ThisWorkbook.Path & Application.PathSeparator & filename1 & ".xlsm"
End Sub
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Hi,
try placing at beginning of your code & see if does what you want

Rich (BB code):
Sub Email1()
    Dim OutApp As Object
    Dim OutMail As Object
    Dim NewWB As String
    Dim filename1 As String
    
    Dim answer As VbMsgBoxResult
    answer = MsgBox("Are you sure you wish to email?", vbOKCancel + vbQuestion, "Continue")
    If answer = vbCancel Then Exit Sub
    
    'rest of code

Dave
 
Upvote 0
Solution
VBA Code:
Sub Email1()
Dim answer As Integer
answer = MsgBox("Are you sure you wish to email", vbOKCancel)
If answer = vbOK Then
 
    Dim OutApp As Object
    Dim OutMail As Object
    Dim NewWB As String
    Dim filename1 As String


    filename1 = Sheet2.Range("AF20").Value & " - " & Sheet2.Range("AC21").Value & " " & Sheet2.Range("AC22").Value & " - " & Sheet2.Range("AC23").Value


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


    'On Error Resume Next
    
    With OutMail
        .To = Range("AJ18").Value
        .CC = "******"
        .BCC = ""
        .Subject = "********"
        .Body = "*********"


        .Attachments.Add ThisWorkbook.Path & Application.PathSeparator & filename1 & ".xlsm"
        
        '.Send
        .Display
    End With
    On Error GoTo 0


    Set OutMail = Nothing
    Set OutApp = Nothing
    
    Kill ThisWorkbook.Path & Application.PathSeparator & filename1 & ".xlsm"






    Else
    MsgBox "Email Cancelled"
End If
End Sub
 
Upvote 0
Hi,
try placing at beginning of your code & see if does what you want

Rich (BB code):
Sub Email1()
    Dim OutApp As Object
    Dim OutMail As Object
    Dim NewWB As String
    Dim filename1 As String
   
    Dim answer As VbMsgBoxResult
    answer = MsgBox("Are you sure you wish to email?", vbOKCancel + vbQuestion, "Continue")
    If answer = vbCancel Then Exit Sub
   
    'rest of code

Dave

Thank you dave. That worked perfectly.

On the box message, I have alot of text, how can I add a line space between the text so i can spread it out a bit more?

Example:
Are you sure you wish to send this email?

(Text Text Text Text Text Text Text Text Text Text Text)

Test.png
 
Upvote 0

Forum statistics

Threads
1,215,563
Messages
6,125,550
Members
449,237
Latest member
Chase S

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