Outlook: VBA explicity specify the account to use via the Accounts drop-down list next to the message's send button

Bering

Board Regular
Joined
Aug 22, 2018
Messages
185
Office Version
  1. 2016
Platform
  1. Windows
Hello,

I have a macro that creates a large number of emails with an attachment. They get saved in the draft folder of an Outllok account, say Account2@mycompany.com, so that they can be reviewed before sending.

I have a total of 3 accounts in Outlook, my personal one, Bering@mycompany.com, set as default, and other two, say Account1@mycompany.com and Account2@mycompany.com

The macro works brilliantly overall, however yesterday there was a problem: the emails appeared to be sent from the correct account, Account2@mycompany.com. However, since I could not find the emails in the outbox of this account, I realised they had been sent from Account1@mycompany.com.

I then realised that by hovering over the account next to the from drop-down list, it actually displays from: Account2@mycompany.com, sent using account: Account1@mycompany.com. In order to have the emails sent from the correct account I had to manually select it from the drop-down list, really annoying.

The code uses .SentOnBehalfOfName = "Account2@mycompany.com". Of course, Account1@mycompany.com does not appear anywhere in my code.

Has anyone experienced a similar issue and found a fix for it? As mentioned in the title, I would like to replicate through VBA the above manual step.

Thank you.
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
I have only just started learning Outlook VBA, but have you tried using .SendUsingAccount instead? Note that you can't use .SendUsingAccount = "Account2@mycompany.com."

You'll have to use an Outlook Account object. Something like below:

EDIT: This is the code I use for one of my Excel to Outlook works. Get rid of the olApp object if you are running the code from Outlook itself.

VBA Code:
    Dim olApp As Outlook.Application
    Dim oAccount As Outlook.Account
    Dim olEmail As Outlook.MailItem
  
    Set olApp = New Outlook.Application
  
    For Each oAccount In olApp.Session.Accounts
  
        If oAccount = "Account2@mycompany.com" Then
      
            Set olEmail = olApp.CreateItem(olMailItem)
          
            With olEmail
  
                .SendUsingAccount = oAccount

                .Display
  
            End With
      
        End If
  
    Next oAccount

End Sub
 
Upvote 0
Solution
I have only just started learning Outlook VBA, but have you tried using .SendUsingAccount instead? Note that you can't use .SendUsingAccount = "Account2@mycompany.com."

You'll have to use an Outlook Account object. Something like below:

EDIT: This is the code I use for one of my Excel to Outlook works. Get rid of the olApp object if you are running the code from Outlook itself.

VBA Code:
    Dim olApp As Outlook.Application
    Dim oAccount As Outlook.Account
    Dim olEmail As Outlook.MailItem
 
    Set olApp = New Outlook.Application
 
    For Each oAccount In olApp.Session.Accounts
 
        If oAccount = "Account2@mycompany.com" Then
     
            Set olEmail = olApp.CreateItem(olMailItem)
         
            With olEmail
 
                .SendUsingAccount = oAccount

                .Display
 
            End With
     
        End If
 
    Next oAccount

End Sub

Thank you very much for your answer. I will give it a try asap (I am currently on vacation) and hopefully it will fix this annoying issue.
Cheers
 
Upvote 0
Just wanted to thank you for your help Vathana.

I finally managed to test your code and it works like a charm (y) (y) (y)

Cheers


Thank you very much for your answer. I will give it a try asap (I am currently on vacation) and hopefully it will fix this annoying issue.
Cheers
 
Upvote 0

Forum statistics

Threads
1,214,572
Messages
6,120,306
Members
448,955
Latest member
Dreamz high

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top