I have below code which changes sender field and also changes account from which the email is being sent. My account is authorized to send also from this email. Code works well on new emails and also works well replied emails, but does not work well on one of the accounts I have. I have 3 accounts (POP, @outlook.com and Exchange). If email has been previously received from an @outlook.com account (this account is also set as exchange account) or set to be sent from an @outlook.com account it does not do the job. For example: If I open new email, set sending account to XX@outlook.com and then run code it does not work. Similar if I reply to an email, which I have received from xx@outlook.com account the code does not work. Code works well if my email (replied or new) is previously set to be sent from my POP account or from my third-exchange account. The code does not work, if my email (replied or new) is previously set to be sent from the @outlook.com account. Why is the code not working on all accounts?
Sub ChangeSender()
Dim NewMail As MailItem, oInspector As Inspector
Set oInspector = Application.ActiveInspector
If oInspector Is Nothing Then
MsgBox "No active inspector"
Else
Set NewMail = oInspector.CurrentItem
If NewMail.Sent Then
MsgBox "This is not an editable email"
Else
NewMail.SentOnBehalfOfName = "test@test.com"
Dim oAccount As Outlook.Account
For Each oAccount In Application.Session.Accounts
If oAccount.DisplayName = "accounttest@accounttest.com" Then
NewMail.SendUsingAccount = oAccount
End If
Next
NewMail.Display
End If
End If
End Sub
In case previously set account is the POP one or the third-exchange account the code works well. In case previously set account is the xx@outlook.com account, the code does not work. In VBA I can see the whole code runs through, but there is no change on the line >>NewMail.SendUsingAccount = oAccount as is in case when working properly. This is my first VBA code, so maybe I am missing some basics.
EDIT: Here is a visual image of what I am trying to do with VBA. Same think as I do with several mouse clicks and some typing: Change the email FROM field and change the account from which I am sending email from. Image
EDIT2: Now I have tried to remove line .SentOnBehalfOfName altogether and the script again works only on POP account and does not work on outlook.com Exchange account. In other words: I left only the line .SendUsingAccount and if email is preset to POP account it changes account correctly, if email is preset to outlook.com exchange account it does not change to the Exchange accounttest@accounttest.com account. Any suggestion why some accounts can be changed to another one, some cannot be changed to another one?
EDIT3: Above is image how I am sending email by hand (mouse/keyboard) with from field to test@test.com and it is being sent through my account accounttest@accounttest.com. I am doing this successfully for several months. For the last 2 weeks I have been using partially successfully above VBA code to achieve the same. VBA code works great if before running the code my email from field is set to my POP account or my Exchange account (accounttest@accounttest.com). Above VBA code does not work in case if before running it, my email from field is set to XX@outlook.com account. There are obviously some restrictions which account can change to which with VBA. When changing by hand, there are no restrictions. I think SentOnBehalfOfName line is not a question any more (it is working great when I change account successfully). Maybe I should make a new question altogether without this line?