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
 
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
 
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
 
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
 
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
 
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.
Similar threads
Thread starter Title 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
V change the Default email Delivery Location Outlook VBA and Custom Forms 3
P Outlook 2016 Change Paste Special Default Format Using Outlook 8
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 Using Outlook 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" Using Outlook 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
H How to make hourglass and change back to default in Outlook class ? Outlook VBA and Custom Forms 3
D How to change account used for replies from default Outlook VBA and Custom Forms 7
C How do I change the default "Add to Outlook Contacts" Folder Using Outlook 6
D RE: How do you change the default insert file path in outlook? Using Outlook 11
C How do I change the default character spacing in Outlook e-mail? Using Outlook 3
M Default settings for "Display as" Change? Outlook VBA and Custom Forms 8
M Use Macro to change account settings Outlook VBA and Custom Forms 0
D Unable to change AppointmentItem.Start property Outlook VBA and Custom Forms 4
sjmo2 Change subject for new e-mails only. Outlook VBA and Custom Forms 2
Horsepower Contact phone numbers randomly change Using Outlook 0
whizzard Change FROM address based on TO or CC address Outlook VBA and Custom Forms 8
S Outlook 365 Can I change the possible range of highlighting colours when writing an Outlook email? Using Outlook 1
V Can one change the formatting of email title blocks? Using Outlook 0
S Unable to change Message Class Outlook VBA and Custom Forms 0
S New Outlook Appointment - Select All Body Text and Change Font and Size Outlook VBA and Custom Forms 1
J VBA Cannot programmatically input or change Value for User Defined field Using Outlook 1

Similar threads

Back
Top