email from macro on close

jsambrook

Board Regular
Joined
Feb 1, 2010
Messages
214
I'm trying to email the spreadsheet on close, but I also have some other code to fit in and I'm not sure where to put it.


Private Sub Workbook_BeforeClose(cancel As Boolean)

x = MsgBox("Have you updated the Version Log", vbYesNo)
If x = vbYes Then Application.Dialogs(xlDialogSendMail).Show
Exit Sub
If x = vbNo Then cancel = True
MsgBox ("Please Update Version Log")
Sheets("Version Log").Select
Range("B5").Select
Do
If IsEmpty(ActiveCell) = False Then
ActiveCell.Offset(1, 0).Select
End If
Loop Until IsEmpty(ActiveCell) = True

Application.Dialogs(xlDialogSendMail).Show
End Sub

I know where the problem is, its the exit sub after the vbYes, It works fine if the answer is yes, it emails fine. But if the answer is no, it still opens the email to send and I dont want this. What is the best way to do it?
thanks
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
You have a second Application.Dialogs(xlDialogSendMail).Show just as you exit the subroutine. This is what's firing when execution is allowed to fall through to the end of the subroutine.

If you set a breakpoint at the start of the code and step though it one statement at a time, you will see where your code execution is going.
 
Upvote 0
Sorry, being a bit thick there! need to have more confidence in myself! I can work these things out! I just need to take a bit more care. Thanks for helping.
Janni
 
Upvote 0
Being able to set breakpoints and step through code is a useful skill to acquire.
 
Upvote 0

Forum statistics

Threads
1,224,588
Messages
6,179,743
Members
452,940
Latest member
rootytrip

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