Change Default Email Account Using VBA

Status
Not open for further replies.
Outlook version
Outlook 2016 32 bit
Email Account
IMAP
Hi all!

I want to automatically select a specific mail account to send e-mails when I am in a specific folder. I understand how to test for the current folder, and I understand how to use VBA to create a new e-mail and send it from a specific account.

However, I want all e-mails sent when I am in a specific folder to be sent using a specific account, which is not the default.

I figure there are at least two ways, and I can't figure out how to implement either of them:
  1. Change the current default account when I enter a specific folder.
  2. Change the account used to send an e-mail using the ItemSend event.
I cannot determine how to simply change the currently selected default mail account (option #1), except when using a macro to actually compose and send a message. When I trap the ItemSend event, it appears that it is too late to change the account used to send the message.

What am I missing? Thanx! :)

hb
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Itemsend has never worked for me when i want to change the account - I need to change the account when I open the message. I can use it to check for the sending account and cancel it if it's a wrong account - i use this method to check the sending account when i send email to a couple of mailing lists that only one address can send to.

What i would do is get the folder that is in focus and use that to determine the sending account as the message is opened. How you do this (If statement, Case, Array) depends on how many folders you need to watch for and how many accounts you need to assign to the folders.

Use this method - Use a Default Subject for New Messages - and change the from account (Macros to send message using the default account or a specific account
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
This is working here is a quickie test with a simple If test - if the folder exists in more than one account, it will apply to all, so using it on the common folders is probably ont going to work. Also, if the folder is in outlook.com account, it will fail (at least if the account is the default) - i only have pop2 and exchange accounts in my profile and did not test it with imap.

Code:
' Based on //www.vboffice.net/en/developers/newinspector-and-inspector-activate
Private WithEvents m_Inspectors As Outlook.Inspectors
Private WithEvents m_Inspector As Outlook.Inspector

Private Sub Application_Startup()
  Set m_Inspectors = Application.Inspectors
End Sub

Private Sub m_Inspectors_NewInspector(ByVal Inspector As Outlook.Inspector)
  Set m_Inspector = Inspector
End Sub

Private Sub m_Inspector_Activate()
If TypeName(m_Inspector.CurrentItem) = "MailItem" Then

' get folder name
Dim objOL As Outlook.Application
Set objOL = Outlook.Application
Set objfolder = objOL.ActiveExplorer.CurrentFolder

'then change account
Debug.Print objfolder
If objfolder.name = "Testing" Then

For Each oAccount In Application.Session.Accounts
   If oAccount = "my@account.net" Then
      m_Inspector.CurrentItem.SendUsingAccount = oAccount
      m_Inspector.CurrentItem.Display
   End If
Next


End If

End If
  Set m_Inspector = Nothing
End Sub
 
Outlook version
Outlook 2016 32 bit
Email Account
IMAP
Thanx, Diane. This works almost perfectly with my IMAP accounts!!

I say "almost" because of one thing. Although this successfully changes the "send from" e-mail account, Outlook does not grab the signature used with that account. How can I get that?

Thanx again!

hb
 
Outlook version
Outlook 2016 32 bit
Email Account
IMAP
Itemsend has never worked for me when i want to change the account - I need to change the account when I open the message. I can use it to check for the sending account and cancel it if it's a wrong account - i use this method to check the sending account when i send email to a couple of mailing lists that only one address can send to.

What i would do is get the folder that is in focus and use that to determine the sending account as the message is opened. How you do this (If statement, Case, Array) depends on how many folders you need to watch for and how many accounts you need to assign to the folders.

Use this method - Use a Default Subject for New Messages - and change the from account (Macros to send message using the default account or a specific account
 
Outlook version
Outlook 2016 32 bit
Email Account
IMAP
Hi!

I figured out everything but how to use a specific signature. Here's the "final" code.

Private Sub Application_Startup()
Set m_Inspectors = Application.Inspectors
End Sub
---------------------------------------------------
Private Sub m_Inspectors_NewInspector(ByVal Inspector As Outlook.Inspector)
Set m_Inspector = Inspector
End Sub
---------------------------------------------------
Private Sub m_Inspector_Activate()

Dim objOL As Outlook.Application
Dim objfolder As Outlook.Folder

' get current folder name
Set objOL = Outlook.Application
Set objfolder = objOL.ActiveExplorer.currentFolder

' if current folder = GCU then contine. if not, loop out
If objfolder.Name = "GCU" Then

' if current e-mail subject is populated, then loop out
If TypeName(m_Inspector.CurrentItem) = "MailItem" And _
m_Inspector.CurrentItem.Subject = "" Then

' choose account GCU to send email
For Each oAccount In Application.Session.accounts
If oAccount = "GCU" Then
m_Inspector.CurrentItem.SendUsingAccount = oAccount
m_Inspector.CurrentItem.Display
End If
Next

End If

End If

Set m_Inspector = Nothing

End Sub
----------------------------------------------------

Thanx for the help!!

hb
 
Status
Not open for further replies.
Thread starter Similar threads Forum Replies Date
E How to display "Change Folder" in Change Default Email Delivery Location in Exchange Outlook 2016 Using Outlook 1
B Change default email for address book Using Outlook 1
K Change default "Display As" for contact email addresses Using Outlook 1
M Change default program for opening email attachments. Using Outlook 1
J When I compose an email in Plain Text, the default alignment is set to the right. How do I change it Using Outlook 1
V change the Default email Delivery Location Outlook VBA and Custom Forms 3
S Outlook 2010 unable to change default font Using Outlook 7
V Change default default save location to Quick Access Using Outlook 1
C Change default "Save Sent Item To" folder Outlook VBA and Custom Forms 9
C Change default colors for conditional formatting Using Outlook 2
Diane Poremsky To Change the Default E-mail Program in Windows New Slipstick.com Articles 0
L Change default from english to danish ...... Using Outlook 5
Davzell Change default pop3 account with script, prf or registery ? Outlook VBA and Custom Forms 0
Diane Poremsky Change the All Day Event Default Free/Busy to "Busy" New Slipstick.com Articles 3
M Change default "automatically test account settings when Next is clicked" Using Outlook 1
David Rahrer Change default Junk E-Mail folder Using Outlook 6
C Change the default sent folder Outlook VBA and Custom Forms 1
JulieB Default data file won't change Using Outlook 6
ogodt Change Default contact form and import from Excel 2010 Using Outlook 1
A iCloud add-on for Outlook: Reminder for new appointment is set to 'None' by default. How to change? Using Outlook 6
J Change Default View - Color and font Using Outlook 3
C Change default reminder time for contacts Using Outlook 2
S CHANGE DEFAULT VIEW FOR CONTACT (People) SEARCH - Outlook 2013 Using Outlook 3
M Change default Current View on People Using Outlook 1
T How to change default search engine in Outlook 2013? Using Outlook 3
J How to change my default calendar...? Using Outlook 1
H Outlook default landscape need to change to portrait Using Outlook 1
K Change the default Archive location via GPO in Outlook 2010 Using Outlook 3
F Change default value of date/time type from "None" to Blank Using Outlook 1
G Default Account Change Using Outlook 5
O Exchange + pst: Can I change the default Outlook calendar/tasks/account? Exchange Server Administration 2
S change default contact list in Outlook 2010 to "contacts" rather than "global" Exchange Server Administration 1
M How do I change the default map from Bing to Google in Outlook 2003? Using Outlook 23
G how to change default calendar view to show this week and future weeks? Using Outlook 1
J How To Change Default View of Tabs in Outlook 2010 Using Outlook 4
C Change default reading font in Outlook from Times New Roman 12 point to Arial Using Outlook 2
S Office 2010 Change Default Views Using Outlook 5
E Change Default Directory when attach or save files of emails Using Outlook 1
M Change the default processing of the Inbox and it's subfolder. Exchange Server Administration 1
S Change default address list Using Outlook 1
B Problems with Default to text and need to change to HTML. Must change Font each time I handle mail or typing. Using Outlook 1
Y Change default location to save VCard Using Outlook 1
R How can I change my default browser Using Outlook 1
R Can you/how do you/change the default Calender folder; MobileMe syncing issue Using Outlook 6
D change default folder-level permissions in exchange Exchange Server Administration 3
H Change Default Outlook Calendar Using Outlook 3
N I can't change from Outlook to Yahoo! Mail as my default. Yahoo! Mail does not show up in the Intern Using Outlook 1
K It won't let me change default data files, and it won't let me remove the current default data file Using Outlook 2
C How to change Outlook 2007 Default Reminder Time Using Outlook 3
M Cannot Change the branding logo in exchange 2010 SP1 owa default theme Exchange Server Administration 6
Similar threads


















































Top