PDA

View Full Version : Need Help with Macro



adjuster
10-08-2014, 09:33 PM
I created a workbook that needs to be submitted via e-mail once completed to a specific user. I am able to e-mail the document; however, I have to manually go in and "run" the macro each time the document is open in order to do so. I tried to do an Auto_Open() macro but that automatically emails the document as it is being opened, rather than allowing someone to complete it and then email it. It has to be something simple I'm missing, but I'm lost. I think I've been looking at it too long. What would I need to add/change so that the submit button within the spreadsheet would be enabled without having to manually choose the macro each time? Thanks in advance.

gmayor
10-08-2014, 09:53 PM
You could run the macro from a button added to the QAT (Quick Access Toolbar); or to the ribbon; or from an activeX button on the worksheet itself, to name a few suggestions.

adjuster
10-08-2014, 11:00 PM
Thanks for your help. I was overthinking the whole thing and did the activeX button. Appreciate your time.

p45cal
10-09-2014, 05:52 AM
Why not somethig like the following in the ThisWorkbook code-module?:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
myAnswer = MsgBox("Email this?", vbYesNoCancel)
Select Case myAnswer
Case vbYes
SendEmail 'the name of the macro that sends the email.
Case vbNo
'do nothing which lets the workbook close as normal.
Case vbCancel
Cancel = True ' aborts workbook closing.
End Select
End Subwhich can be shortened to:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Select Case MsgBox("Email this?", vbYesNoCancel)
Case vbYes: SendEmail 'the name of the macro that sends the email
Case vbCancel: Cancel = True ' aborts workbook closing.
End Select
End Sub