Change Default Email Account Using VBA

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
 

Top