Log in

View Full Version : outlook VBA dialog box when sending email



SvenBlomme
11-14-2014, 02:22 AM
Hi everyone,

I have not found a similar thread though I assume there is something in this forum I could probably use.
I am familiar with VBA for Excel but not for Outlook.

I would like to write a script which does the following:
- when I send an email I want Outlook to fire a dialog box asking a yes/no question (e.g. 'are you sure you entered all correct recipients?')
When answering YES: send email
when answering NO: go back to message, don't send yet and allow me to update the required recipients

I assume this is not too difficult, but as I am not at all familiar with VBA for Outlook, I think this is a much faster way to do this.

Thanks a lot for your help!

Best,
sven

gmayor
11-14-2014, 08:46 AM
You would use the itemsend event e.g. put the following in the ThisOutlookSession module.

Option Explicit
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim Prompt As String
Prompt = "Are you sure you entered all correct recipients?"
If MsgBox(Prompt, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check for recipients") = vbNo Then
Cancel = True
End If
End Sub

SvenBlomme
12-16-2014, 06:29 AM
Thanks a lot, this surely works very well! My code now reads:

Option Explicit
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)

Dim Prompt As String
Prompt = "Did you Bcc the study account?"
If Item.BCC = "" Then
If MsgBox(Prompt, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "BCC study account") = vbNo Then
Cancel = True
End If
End If

End Sub


However, though this works like a charm when sending emails, I get a runtime error 438 when I create (or reply to) meeting requests etc. I have tried to find a way to overcome this, but without success so far (clicking END solves the problem so there is no real issue as such, but it would be nice if I could prevent runtime errors from happening when I work in my calendar).

Does anyone know how I can prevent this from happening?

Charlize
12-16-2014, 04:10 PM
Check if the item is an email or not

If Item.Class = olMail Then

stuff to do

End If
Charlize

SvenBlomme
02-12-2015, 02:34 AM
Hello,

Thanks for all your help, it works almost perfectly.
The only thing that I notice from time to time is that, when I start up my computer (and Outlook) in the morning, every now and then this macro doesn't seem to fire. This doesn't happen every day (I would say about once every 3 weeks), and I can solve it by going into the VB editor, double click on ThisOutlookSesseion, press save and exit the VB editor.
Do you know why this happens? Ideally I would like this to work every time when I strat my computer and Outlook, not in 'only' 95% of the time...
I have looked on the web here and there and found some info relating to initializing handler, but I don't know if this is the answer to my problem (it happens rarely so I have no way of knowing whether everything works because of the 'initialize handler' or because it's not one of those 'bad' days).

Thanks a lot,
sven