Macros to send message using the default account or a specific account

Last reviewed on October 13, 2013

Macro using the Default Account

An IMAP user created this macro to open a new message form from any message store and use the default account as assigned in Account Settings.

To use: Go to File, Options, Customize Ribbon. Select Macros from Choose Commands from dropdown, add a New Group to Home tab then add the New Mail macro to the new group. Click the Rename button to rename the command and choose a better looking icon.

Text file containing both macros on this page.


Public Sub New_Mail()
Dim olNS As Outlook.NameSpace
Dim oMail As Outlook.MailItem

Set olNS = Application.GetNamespace("MAPI")
Set oMail = Application.CreateItem(olMailItem)

'use first account in list
    oMail.SendUsingAccount = olNS.Accounts.Item(1)
    oMail.Display
      
Set oMail = Nothing
Set olNS = Nothing

End Sub

See Using VBA Codeif you need help using VBA code.

Macro using a specific account

This macro is assigned to a button on the ribbon - clicking the button selects the account listed in the code. Replace Name_of_Default_Account with your account name (check in Account settings for the account name). You can create a macro for each account (change the Public Sub name) if desired.

Go to File, Options, Customize Ribbon. Select Macros from Choose Commands from dropdown, add a New Group to Home tab then add the New Mail macro to the new group. Click the Rename button to rename the command and choose a better looking icon.

If you need help customizing the ribbon, see Customizing the Quick Access Toolbar (QAT)

See Using VBA Code if you need help using VBA code.
Text file containing both macros on this page.

Public Sub New_Mail()
Dim oAccount As Outlook.Account
Dim oMail As Outlook.MailItem

For Each oAccount In Application.Session.Accounts
   If oAccount = "Name_of_Default_Account" Then
      Set oMail = Application.CreateItem(olMailItem)
      oMail.SendUsingAccount = oAccount
      oMail.Display
   End If
Next
End Sub

Note: this macro also works with Outlook 2007 (possibly older versions).

Set the From address on a message

Similar to the previous macro, except it uses a different From address, not a different email account in your profile. Because the message is sent from the default email account, the default account needs Send As permission for the address.

If the account does not have Send as permission, the message will be sent from the default account on behalf of the address or will bounce if using Exchange server. If you are using a SMTP server the message may be sent from the default account.

Use this code to fill in the From field with an address you have permission to send messages From.

Public Sub CreateNewMessageFrom()
Dim objMsg As MailItem

Set objMsg = Application.CreateItem(olMailItem)

 With objMsg
    .SentOnBehalfOfName = "alias@domain.com"
    .BCC = "alias2@domain.com"
    .Display
End With

Set objMsg = Nothing
End Sub

Send a meeting request using a specific account

Use this macro to send meeting requests using a specific account, irregardless of which data file you are viewing.

Public Sub NewMeeting()
Dim oAccount As Outlook.Account
Dim oMeeting As Outlook.AppointmentItem

For Each oAccount In Application.Session.Accounts

If oAccount = "account@displayname" Then
    Set oMeeting = Application.CreateItem(olAppointmentItem)
       oMeeting.MeetingStatus = Outlook.OlMeetingStatus.olMeeting
      oMeeting.SendUsingAccount = oAccount
    oMeeting.Display
End If
Next

End Sub

Using VBA Code

To use VBA code, press Alt+11 to open the VBA Editor. Locate ThisOutlookSession and paste the code into the editor.

Press F5 or the Run button to test the macro. (It's highly recommended you make a backup of the folder or message store before running macros.)

You'll also need to change macros security or use selfcert.exe to sign your macros. Access the dialog to change the security level from Tools, Macros, Security. (This is at File tab, Options, Trust Center, Macro Security in Outlook 2010). Set it on "always ask". Do not choose the Low option (run all, never ask). Some security software will set it to High and your macros will not run.

To run a macro later, press Alt+F8 to open the macro dialog, then select the macro and choose Run. Or add a button for the macro to your toolbar or add it to the QAT in Outlook 2010.

More Information

How to Enable the Developer Ribbon
See How to use VBA code samples in Outlook for more detailed information on using macros and selfcert.

Written by

Diane Poremsky
A Microsoft Outlook Most Valuable Professional (MVP) since 1999, Diane is the author of several books, including Outlook 2013 Absolute Beginners Book. She also created video training CDs and online training classes for Microsoft Outlook. You can find her helping people online in Outlook Forums as well as in the Microsoft Answers and TechNet forums.

10 responses to “Macros to send message using the default account or a specific account”

  1. Robert

    I want to thank you so much for this, I have searched literally for years on how to email from specific accounts without having to remember to switch accounts in the email. Such a lifesaver.

  2. Dave Englund

    Diane, thanks for providing these code snippets! Now, I have a question :D

    We currently use a bit of code called "ReplyWithAttachments" because Outlook does not include attachments in a Reply All, and our Help Desk needs to include the original attachments on all our correspondence. So, we use this nifty "ReplyWithAttachments" code to do that. Now, what I'm wondering is, would you be able to incorporate your "using a specific account" code into our "ReplayWithAttahments" code? This would solve some problems for us, in that we could ensure that when we use this our "From" will always be the Help Desk address rather than our personal address. Here's the "ReplyWithAttachments" code:

    '
    Sub ReplyWithAttachments()
    ' Keyboard Shortcut: Ctrl+w
    Dim rpl As Outlook.MailItem
    Dim itm As Object

    Set itm = GetCurrentItem()
    If Not itm Is Nothing Then
    Set rpl = itm.ReplyAll
    CopyAttachments itm, rpl
    rpl.Display
    End If
    Set rpl = Nothing
    Set itm = Nothing
    End Sub

    Function GetCurrentItem() As Object
    Dim objApp As Outlook.Application

    Set objApp = CreateObject("Outlook.Application")
    On Error Resume Next
    Select Case TypeName(objApp.ActiveWindow)
    Case "Explorer"
    Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1)
    Case "Inspector"
    Set GetCurrentItem = objApp.ActiveInspector.CurrentItem
    Case Else
    ' anything else will result in an error, which is
    ' why we have the error handler above
    End Select
    Set objApp = Nothing
    End Function

    Sub CopyAttachments(objSourceItem, objTargetItem)
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fldTemp = fso.GetSpecialFolder(2) ' TemporaryFolder
    strPath = fldTemp.Path & "\"
    For Each objAtt In objSourceItem.Attachments
    strFile = strPath & objAtt.FileName
    objAtt.SaveAsFile strFile
    objTargetItem.Attachments.Add strFile, , , objAtt.DisplayName
    fso.DeleteFile strFile
    Next
    Set fldTemp = Nothing
    Set fso = Nothing
    End Sub
    '

  3. FP

    Thank you for this. Very helpful. Only trouble I had was with the instructions on changing macro security for the 2010 version. I found it at File Tab/Options/Trust Center/Trust Center Settings/Macro Settings. And then I selected Notifications for all macros.

  4. JDL

    I am truly having a hard time with this.

    I run Outlook 2013 against Exchange server.

    I am connected to two Exchange accounts. My personal one is just my name at my instution (jlevine@acme.edu), and I was recently made the coordinator for another department and given full permissions over the department's e-mail (LAP@acme.edu).

    I am trying to set up User-Defined Forms that allow me to send various form e-mails, form meeting requests, and things of the sort very quickly, so I am saving them as .OFT files and I am using your simple macro and attaching those to buttons on a custom ribbon to call them up in a single click! For routine E-MAIL messages, it works like a charm. I created them while working in the LAP account, and when I call it up, it comes up automatically as being sent FROM the LAP account (just the way I want it!)

    For MEETING REQUESTS, though, not so much. I CREATED the .OFT while working in the LAP account, then saved the macro and attached it to a button, but when I call up the meeting request macro, there is virtually NO "From" option. In fact, it's blank. When I do send that meeting request to its intended recipients, it shows up in their email as coming from "jlevine@acme.edu ON BEHALF OF LAP@acme.edu." I cannot change this at all. There's no option to change the "From" account.

    The funny thing is that when I create a meeting request MANUALLY, going through the steps from scratch, I can send the request from whatever account I want. It's ONLY when I create an .OFT template that it won't let me choose the FROM account.

    What is it I'm not seeing/knowing/doing right?!?! This is frustrating!

    Thank you for whatever feedback you afford me in advance!

  5. Michael

    Thank you so much for this VBA code. I have been searching for days for this.

  6. Diane Poremsky

    Are you using the meeting request code?

  7. Suraj Singh

    HI,

    I am new to VBA and recently started working on Macros with Excel.

    I have a problem. I have two outlook Profiles say Profile A and Profile B. When I created a macro in the Excel using VBA, with specific user ID's who belong to Profile B. I have also created a button in Excel sheet which when clicked will open an Outlook email with those ID's in "cc" column. Now, my problem is when I click on the create button in Excel sheet Profile A's Outlook is being generated and the user ID's do not belong to Profile A. How can I change the Outlook profile in VBA or Excel or set the outlook profile name to a specific profile. Kindly help.

Leave a Reply

If the Post Coment button disappears, press your Tab key.