Outlook 2007 - Prompt for Mail Account when Sending Email

Daniel Mitchell 03/08/2007

Like many people I have my outlook configured with multiple mail accounts, one for personal, one for work and a couple for my freelance work. However when you send an email, Outlook assume you want to send it using your default mail account. This can often mean that you send email using the wrong address which can cause problems.

Now, I know you can easily change the Mail account by clicking on the Account button beneath the Send button. However it is very easy to forget or think you have it set right, only to find out later it was wrong. However what I really want is for outlook to prompt me every time I send an email, thus I will never forget and it should stop me from using the wrong account.

After reading many forum posts by people searching for a solution without success, I decided to write code this myself. This is the first time I have programmed Outlook before. But the results are good.

Popup box with mail accounts

Now whenever I click send I get a small popup form appear (See above) which lists my mail account. It will highlight the current default account, but I can then select a different account if required. Once I press enter or click send its done. Or what is equally convenient is the second chance to cancel.

So how does it work?

Well the first thing to note is that this solution only works for Outlook 2007 or newer as it makes use of the “SendUsingAccount” method, which is new to Outlook 2007.

I then use the function call “Private Sub Application_ItemSend(ByVal item As Object, Cancel As Boolean)” to interrupt the sending of the email, to allow me to open up the form which gets the list of accounts.

I have also added a couple of extra bits of code (while I was on a roll) to prevent me from sending emails without a subject. It also checks to see if I have used the word “attach” in my email and if there are no attachments it will ask me if I meant to send the email without any attachments.

One other setting I had to change, was the security settings, as the code would not run unless I had set the security level to “No security check for macros”. (This is done by going to Tools -> Macros -> Security) - As long as you do not make a habit of installing macros you should be safe, but if anyone has a way of making this code work without having to disable macro security I would be glad to hear from you.

The Code:

ThisOutlookSession

Public blnSend As Boolean

Private Sub Application_ItemSend(ByVal item As Object, Cancel As Boolean)
    If TypeOf item Is Outlook.mailItem Then
        blnSend = False
        Load frmAccountList ' Load account list   form
        frmAccountList.Show
        If blnSend Then
            ' Check that the message has a subject - Compulsory!
            If item.Subject = "" Then
                MsgBox "You forgot the subject."
                Cancel = True
            Else
                ' Check that attachment has been added
                If InStr(1, item.Body, "attach", vbTextCompare) > 0 Then
                    If item.Attachments.Count = 0 Then
                        ans = MsgBox("There is no attachment, send anyway?", vbYesNo)
                        If ans = vbNo Then
                            Cancel = True
                        End If
                    End If
                End If
            End If
        Else
            Cancel = True
        End If
    End If
End Sub

frmAccountList

Create a New Form, and layout the two buttons and list box.

frmAccountList Code

Private Sub butCancel_Click() frmAccountList.Hide End Sub Function changeAccount() Dim item As mailItem Set item = Application.ActiveInspector.CurrentItem ' Change Mail Account item.SendUsingAccount = Application.Session.Accounts(lstMailAccounts.Value) ' Enable Sending ThisOutlookSession.blnSend = True frmAccountList.Hide End Function Private Sub butSend_Click() Call changeAccount End Sub Private Sub lstMailAccounts_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) If KeyAscii = 13 Then ' On Enter Key Press Call changeAccount End If End Sub Private Sub UserForm_Initialize() Dim item As mailItem Set item = Application.ActiveInspector.CurrentItem Dim oAccount As Outlook.Account For Each oAccount In Application.Session.Accounts If oAccount.AccountType = olPop3 Then lstMailAccounts.AddItem (oAccount) If oAccount = item.SendUsingAccount Then lstMailAccounts.Value = oAccount End If End If Next End Sub

End Sub


And thats it….


17 Comments

  1. Gordon Thompson Says:
    November 16th, 2007 at 2:37 pm

    Hi Daniel, the code looks very handy. Can you email me the form and the code?

    Cheers

  2. Gordon Thompson Says:
    November 16th, 2007 at 3:34 pm

    It might be handy to point out that the code needs to be put into ThisOutlookSession.

    I’ve managed to get it working, many thanks for sharing the useful code.

  3. Daniel Mitchell Says:
    November 16th, 2007 at 6:52 pm

    I recieved an email from Scott about how to make this code work without having to adjust the Security Level.

    Simply follow the instructions here: http://office.microsoft.com/training/Training.aspx?AssetID=RP011616191033&CTT=5&Origin=RP011616211033 to create a self signed certificate for the code.

    Thanks Scott

  4. Tommy Says:
    December 7th, 2007 at 12:21 am

    Daniel,

    Have been searching for this for ages…..

    For us non-techies would anyone be able to give some dummies guide instructions on how to get this all working?

    Regards,

    Tommy

  5. Nick in Atlanta Says:
    January 22nd, 2008 at 1:34 am

    This is brilliant. I had trouble getting it to work, but figured it out. I am not a programmer so that was the biggest hurdle.

    Here are a few pointers for those like me:

    You must name the following items correctly:
    Form= frmAccountList
    Listbox = lstMailAccounts
    Cancel Button = butCancel
    Send Button = butSend

    These are named on the first property attribute, ” (Name)”

    I had several syntax errors. I finally figured out it was because when I copied and pasted the code the ” and ‘ symbols were not correct for comments and quotes. I had to go through all code and retype those from my keyboard. Comments showed up a green text and quotes where in black.

    In case anyone is wondering the “Attach” check does catch all variations of the word. In other words if you use “Attach”, “Attached”, “Attachment” you will get the message box.

    Brilliant code, thank you.

  6. Russ Says:
    January 23rd, 2008 at 8:47 pm

    Hi,

    I’m dying to give your solution a whirl. Can you please forward to me the form and code?

    Thanks

  7. Russ Says:
    January 23rd, 2008 at 9:38 pm

    Thanks Michael…I got it to work.
    One question. I have four account types…pop3, imap, ms exchange, hotmail (http). The project above only displays the pop3 and imap accounts. MS Exchange and HTTP are missing. Any ideas?

  8. Russ Says:
    January 23rd, 2008 at 9:42 pm

    Sorry Daniel…..don’t know why I called you Michael.

    Russ

  9. Nick in Atlanta Says:
    January 24th, 2008 at 11:08 pm

    One simple change request. I wish I knew how to program or I would try it myself. The way it works now is you click an account from the list and then click the Send button. Is there any way to program a double click on a list item that selects and sends at the same time? I keep finding myself trying to do this.

  10. Daniel Mitchell Says:
    January 24th, 2008 at 11:32 pm

    Hello Nick, Russ (and others)

    I have been getting many request for various tweaks and adjustments to the code. Although I am afraid I dont seem to get the time that often. So I apologise if Iam slow in responding. However I did a little bit of work on it tonight and have the following adjustments.

    @Nick: Add the following code to frmAccountList to make the sending process work when you double click an entry.

    Private Sub lstMailAccounts_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
    	Call changeAccount
    End Sub

    @Russ: After a bit of research I discovered the other mail account types are: olExchange, olImap, olPop3, olHttp, or olOtherAccount. So if you want to show the other accounts you will need to change this line:

    If oAccount.AccountType = olPop3 Then

    to:

    If oAccount.AccountType = olPop3 or oAccount.AccountType = olExchange Then

    Or you could possibly try just deleting that line to show all of them (make sure you delete its corresponding End If statement though).

    Maybe one day I will get this packaged up into a nice installer to make it easy to setup. (And fix some of the rough edges and occassional bugs.)

    Thanks for your feedback and comments. I appreciate it.

  11. Phil Says:
    January 29th, 2008 at 11:43 am

    Hi,

    Just what i have been looking for, but any way to do this in OL2003?

    Thanks

    Phil

  12. Sameer Says:
    February 15th, 2008 at 2:41 am

    Hi Daniel,

    Really a good code. I have some issue. The error is on this line

    ThisOutlookSession.blnSend = True

    Following is the error which says

    —————————
    Microsoft Visual Basic
    —————————
    Compile error:

    Method or data member not found
    —————————
    OK Help
    —————————

    I use Windows Vista Ultimate and Outlook 2007. Any help would be appreciated.

    Thanks and regards,
    Sameer.

  13. Sameer Says:
    February 15th, 2008 at 3:39 am

    Daniel,

    Please ignore my earlier comment about the error which is now resolved. i forgot to put the first line. Once again thanks for this great code made my life easy.

    Regards,
    Sameer.

  14. Chris Miller Says:
    February 19th, 2008 at 9:03 pm

    Thanks all! This is exactly what i was looking for. It works great.

  15. Iain Says:
    March 5th, 2009 at 10:46 am

    Hi Daniel,
    I’d really love to be able to do this.
    I also am not a programmer, could you send me the form and code.
    Would I need to have something like Visual studio installed to do thisor can I do it in Outlook?
    Any chance of an “Idiots guide - step by step”?
    much appreciated

  16. Iain Says:
    March 5th, 2009 at 12:34 pm

    actually did it - hooray!
    I found the visual basic editor in Outlook and also took note of Nicks comments about the quote marks etc and it’s all fine now.
    cheers daniel

  17. Iain Says:
    March 5th, 2009 at 12:39 pm

    I have noticed a problem though.
    It doesn’t pick up the correct signature for each account. It just goes with the default one..Any way around that.

 

Have a comment or question to make? Email: me [at] daniel-mitchell.com.