VBA to send email if vbQuestion=Yes

bubbazgirl1

New Member
Joined
Aug 31, 2011
Messages
3
Hello -

I am new to VBA and I am attempting to create VBA to send an email if the user selects Yes for a vbQuestion. I was able to have an email created and sent using the code below; however, the email still sends even if the user selects no. I am not sure where the code is going off the path.

' Request to send email to management with revised metrics information
Dim response3 As VbMsgBoxResult
Dim Email_Subject, Email_Send_From, Email_Send_To, Email_Cc, Email_Bcc, Email_Body As String
Dim Mail_Object, Mail_Single As Variant

response3 = MsgBox("Do you wish to send email with attachments to management?", vbQuestion + vbYesNo, "Email Managment")
If response2 = vbYes Then
Email_Subject = "Test - Weekly Metrics " & Format(Date, "mm/dd/yyyy")
Email_Send_From = "person@gmail.com"
Email_Send_To = "person@gmail.com"
Email_Cc = "person@gmail.com"
Email_Bcc = "person@gmail.com"
Email_Body = "Hello - " & vbCr & vbCr & "I have attached the most recent version of the weekly metrics." & vbCr & vbCr & "Thanks," & vbCr & vbCr & "Jane"

On Error GoTo debugs
Set Mail_Object = CreateObject("Outlook.Application")
Set Mail_Single = Mail_Object.CreateItem(0)
With Mail_Single
.Subject = Email_Subject
.To = Email_Send_To
.cc = Email_Cc
.BCC = Email_Bcc
.Body = Email_Body
.attachments.Add ("G:\***\*** " & Format(Date, "mm.dd.yyyy") & ".xlsx")
.attachments.Add ("G:\***\***2 " & Format(Date, "mm.dd.yyyy") & ".xlsm")
.send
End With
debugs:
If Err.Description <> "" Then MsgBox Err.Description
' Message to advise email sent.
MsgBox "An email(s) has been succesfully sent to the appropriate managment personnel.", vbExclamation

Else
MsgBox "Email not sent to management."
End If

I am having a similar problem with the IF statement below. If no the code is still closing the application instead of just ending the SUB.

' Determine if the user would like to close Excel.
Dim response4 As VbMsgBoxResult
repsonse4 = MsgBox("Would you like to close the document?", vbQuestion + vbYesNo, "Close Document?")
If response4 = vbNo Then Exit Sub
Application.Quit ' Close Excel completely
End Sub

I would appreciate any assistance anyone could provide!!

Thanks!
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Hi Andrew,

Thank you!!! I was so close. Sometimes it's the most obvious things that you over look. :oops:

Do you have any suggestions for the 2nd question in regards to only closing the application if the user clicks Yes?
 
Upvote 0
In your second part, you have a typo:

Rich (BB code):
repsonse4 = MsgBox("Would you like to close the document?", vbQuestion + vbYesNo, "Close Document?")
 
Upvote 0
You are testing for repsonse4 instead of response4. I strongly recommend that you put Option Explicit at the top of your module. This forces you to declare all variables and typos will give rise to a compile error.

You can have it added automatically to all new modules by checking Require Variable Declaration under Tools|Options|Editor tab.
 
Upvote 0
Thank you both!!!

Thank you for the tip about the Option Explicit and how to add it automatically. I will definitely do add to prevent any typo like errors in the future. :)
 
Upvote 0

Forum statistics

Threads
1,224,502
Messages
6,179,126
Members
452,890
Latest member
Nikhil Ramesh

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