VBA - How to include a popup message to confirm before executing the rest of the Macro?

VorLag

Board Regular
Joined
May 13, 2013
Messages
205
Hello, board!

I am working on refining my macros... which you all have helped me develop and perfect. Now, I would like to add one last function to the end of them that will automatically send the resulting report to the people who need to get it after it finishes saving. However, I have made the mistake of running the wrong Macro on a particular dataset, so I don't want my mistake blasted out to everyone in the universe. What I'd like to do is insert a command that will ask for me to confirm that I am peachy-keen before sending the file out, but I am not sure how to do that. Can someone tell me how to edit this to include a mandatory confirmation selection before going onto the outlook part of this macro?

I think all of the code is correct, but I haven't actually tried it yet. Also, the code to create the report was condensed as I don't think it would be terribly relevant to anyone else. I've only included the very last part for saving - which I know works - and the new part for the outlook mail, which I haven't tested yet.



The file saving part is put here because I need a particular date range in the file to appear in the file name along with the current date.
Code:
    Dim Path As String
    Dim FileName1 As String
    Dim FileName2 As String

    Path = "C:\Documents and Settings\user\Me\Reports\Sample Report"

    FileName1 = "(" & Range("C3") & ")"
    FileName2 = Format(Now(), "mmddyy")

    ActiveWorkbook.SaveAs Filename:=Path & "_" & FileName1 & "_" & FileName2 & ".xls", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
    
    Rows("1:5").Select
    Selection.EntireRow.Hidden = True
   

[[I'd like to have the popup message go here]]


Dim OutApp As Object
Dim OutMail As Object

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

On Error Resume Next
With OutMail
.TO = "email@email.com;emailxyz@email.com"
.CC = "lorax@snoozing.com"
.BCC = 
.Subject = "Sample Report"
.Body = "Hi%20All!%0A%0AAttached%20is%20the%20report%20for%20today.%20Let%20me%20know%20if%20you%20need%20anything%20else!%0A%0ALove,%0AVorlag"
.Attachments.Add ActiveWorkbook.FullName
.Display
End With
On Error Goto 0

Set OutApp = Nothing
Set OutMail = Nothing
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
At the end of your code for the Outlook message, it says

.Display

Not .Send.

So, the user will still need to hit the send button in Outlook. You don't really need to add this functionality to your code. But if you want something like this would work.

Code:
If MsgBox("Send Message", vbYesNo) = vbYes Then
    'code to send message
Else
    Exit Sub 'terminate macro
End If
 
Upvote 0
You could try something along these lines:

<font face=Calibri><SPAN style="color:#00007F">Sub</SPAN> foo()<br>    <SPAN style="color:#00007F">Dim</SPAN> ans <SPAN style="color:#00007F">As</SPAN> VbMsgBoxResult<br>    <br>    ans = MsgBox("Do you want to go ahead and send the report now?", vbQuestion + vbYesNoCancel, "Send Report?")<br>    <br>    <SPAN style="color:#00007F">If</SPAN> ans = vbYes <SPAN style="color:#00007F">Then</SPAN><br>           <SPAN style="color:#00007F">Call</SPAN> SendMessage<br>    <SPAN style="color:#00007F">Else</SPAN><br>        <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    <br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>

HTH,
 
Upvote 0
At the end of your code for the Outlook message, it says

.Display

Not .Send.

So, the user will still need to hit the send button in Outlook. You don't really need to add this functionality to your code. But if you want something like this would work.

Code:
If MsgBox("Send Message", vbYesNo) = vbYes Then
    'code to send message
Else
    Exit Sub 'terminate macro
End If



Oh, I was wondering about that. Thanks for pointing that out!
 
Upvote 0
You could try something along these lines:

Sub foo()
Dim ans As VbMsgBoxResult

ans = MsgBox("Do you want to go ahead and send the report now?", vbQuestion + vbYesNoCancel, "Send Report?")

If ans = vbYes Then
Call SendMessage
Else
Exit Sub
End If

End Sub


HTH,

Good to know! Thank you!
 
Upvote 0

Forum statistics

Threads
1,213,536
Messages
6,114,208
Members
448,554
Latest member
Gleisner2

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