Search code examples
vbaoutlookscriptingstartup

VBA script in Outlook that runs when an email is sent


I've got someone that needs a dialog box to popup whenever they hit "Send" in Outlook. I found the below VBA code that does something similar by asking the user if they BCC'd someone:

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

But in playing with this, I can only get it to work if I set it up manually each time Outlook opens. I want Outlook to open and ready the script to be run whenever the user goes to compose an email.

I tried playing around with the VBA editor, but my knowledge of it is slim. I didn't quite understand how things get passed to each other, and it seems different than other scripting languages to me.


Solution

  • For those who may find this, I found an answer in this thread.

    The top answer described it correctly. There were extra bits of code in the ThisOutlookSession object whenever I put in my code. I was trying to put my code within a sub that was already created for the module, which messed things up. I cleared it completely and used this as a sample:

    Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
    Dim prompt As String
    prompt = "Are you sure you want to send " & Item.Subject & "?"
    If MsgBox(prompt, vbYesNo + vbQuestion, "Sample") = vbNo Then
        Cancel = True
        End If
    End Sub
    

    After setting permissions to allow scripts to run, it now shows a textbox that will verify if you want to send an email, which is what I wanted to happen.